Search code examples
javafinal

IntelliJ variable might not have been initialized


So the first thing I want to make clear is that I think this is my error that I'm just failing to see, but I figured it could help to know the IDE is IntelliJ. Also, I've been looking at other overflow posts(question 1, final keyword, question 2 to name a few) but they haven't answered my question as far as I can tell. I've only had AP computer science as my education in CS, so my knowledge is very limited.

The issue is with a non-public class in a file. It creates generates a path through a 2d array, and it can create instances of itself for a branching effect, but doesn't have to. Also, multiple versions of this will be created by the public class in the file. Below is my code(the parts I think are relevant. If you need more, let me know and I will update my question). Let me know if there is anything else you need, and thank you for helping!

public class PathMaker
{
    ....
}

class RiverPath
{

    private final int startID;
    private final int endID;
    private final double branchChance;
    private final double endFactor;

    public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
    {
        startID = MapNode.createID(x, y);
        branchChance = bChance;
        endFactor = eFactor;

        ...
        endID = a number;
    }
    public RiverPath(int x, int y, int[][] alts, double bChance)
    {
        this(x, y, alts, bChance, 0, .02, -1, -1);
    }
    ...
}

There are no other constructors, and MapNode is another class with the public static int createID(int x, int y) method.

Let me know anything I need to do to make my question clearer.


EDIT: All 4 variables are giving me grief. Also, I'll put the full constructor below. As far as I can see, there is no return statement in my code. Also, the error is before I compile and says

Variable [name] might not have been initialized

These 4 errors are the only ones. There are a few variables who are defined outside the constructor.

public RiverPath(int x, int y, int[][] alts, double bChance, int c, double eFactor, int pX, int pY)
{
 count = c;

 if(c > 0)
    isBranch = true;
 else
    isBranch = false;//pX and pY only matter if is Branch

 startID = MapNode.createID(x, y);
 branchChance = bChance;
 altitudes = alts;
 endFactor = eFactor;

 mainPath.add(new MapNode(x, y, altitudes));

 boolean pathing = true;
 MapNode currentNode = mainPath.get(0);
 int[][] heights = new int[3][3];//heights around river
 boolean[][] heightTight = new boolean[3][3];
 int min;
 int minCount;
 int tID;
 RiverPath branch;

 while(pathing)
 {
    if(Math.random() < endFactor*count)
    {
       pathing = false;
    }
    else
    {
       count++;
       min = 99;
       minCount = 0;

       for(int i = -1; i < 2; i++)
       {//These loops fill heights with the nearby heights of mapnodes and set min as the min
          for(int z = -1; z < 2; z++)
          {
             heights[i+1][z+1] = altitudes[currentNode.getY() + i][currentNode.getX() + z];
             if(heights[i+1][z+1] < min)
             {
                min = heights[i + 1][z + 1];
             }
          }
       }

       if(min == currentNode.getAltitude())
       {
          min = 0;
          for(int i = -1; i < 2; i++)
          {
             for(int z = -1; z < 2; z++)
             {
                tID = MapNode.createID(currentNode.getX() + z, currentNode.getY() + i);
                if(heights[i+1][z+1] == currentNode.getAltitude() && (!isBranch || !(MapNode.createID(pX, pY) == tID)) && (mainPath.size() == 1 || mainPath.get(mainPath.size() - 1).getID() != tID))
                {//if the altitude is the min, and it either isn't a branch or it isn't the node before this one
                   heightTight[currentNode.getY()+i][currentNode.getX()+z] = true;//a possible path exists here
                   min++;//min now keeps track of the total number of possible paths there are
                   minCount++;//also keeps track of total, but for a different implementation later
                }
             }
          }
          while(min != 0)
          {
             if(min == -1)
                min = -2;//signals that we can test branches
             for (int i = -1; i < 2; i++)
             {
                for (int z = -1; z < 2; z++)
                {
                   if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0/)//
                   {

                      if(min > 0)
                         min = -1;//signals that we can skip all other true values, but ONLY if there are more possible branches
                      else
                         min = 0;//in case we lower min below 0
                   }
                   else if(min == -2 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < branchChance)//both random chance and it is a possible path
                   {
                      branch = new RiverPath(currentNode.getX() + z, currentNode.getY() + i, altitudes, branchChance, count, endFactor, currentNode.getX(), currentNode.getY());
                      branches.add(branch);
                   }
                }
             }
          }
       }
    }
 }

 endID = currentNode.getID();
}

Solution

  • Going off the snippet you've shared, my guess is you have a conditional return somewhere in that ... in your constructor. Since you return early, endID may not be set. This is a common cause of this error.


    Edit:

    With the larger code snippet you posted I'm able to replicate your issue in IntelliJ, and I see an additional error "Expression expected" on this line:

    if (min > 0 && heightTight[currentNode.getY() + i][currentNode.getX() + z] && Math.random() < 1.0 /)//
    

    This (specifically the trailing / after 1.0) appears to be your real issue - the "might not have been initialized" errors are simply symptoms of your constructor being malformed.