-3

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();
}
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 4
    Sharing an [MCVE](https://stackoverflow.com/help/mcve) is very helpful for these sorts of questions; if you omit part of the code that's causing your problems it makes it difficult to diagnose the issue. – dimo414 Jul 03 '17 at 03:41
  • Which variable?? – OneCricketeer Jul 03 '17 at 03:43
  • 1
    Missing : the error message! – GhostCat Jul 03 '17 at 03:52
  • If I actually fix the compilation errors with your code snippet (with dummy data, naturally), I don't see any issues with your final variables. You need to explain your problem *clearly* and *concisely*, with code to support the issue. Without it, we're taking stabs in the dark, which is a waste for everyone involved. – Makoto Jul 03 '17 at 03:54
  • I believe it would be worth to break this code up in smaller parts. The main issues you are facing is likely that you have one big function doing a lot of things. This makes it hard to predict which variables needed in every step. Apart from this, it is easier to keep track of all variables and cases. Another advantage with smaller functions, which have one purpose, is that they are easier to read (eg. for your tutor and later to remember what you did) – patrik Jul 03 '17 at 04:48
  • @patrik thank you for the advice, I'll try to break up my constructor more and limit my method size in the future. – Kieran Edraney Jul 03 '17 at 15:48
  • @KieranEdraney You should do it now. This will help you get rid of your error. It is either that or spending the same amount of time (if not more) on troubleshooting. – patrik Jul 03 '17 at 21:44

1 Answers1

-2

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • The snippet will work fine if the immediate compilation issues are addressed. It's unclear what's causing it from just what the OP has provided, so let's not guess. – Makoto Jul 03 '17 at 03:55
  • You were right, I missed the division sign. Sorry I didn't upload enough code to start with, and thank you for bearing with me! – Kieran Edraney Jul 03 '17 at 15:46
  • No problem, now you know :) Making an [MCVE](https://stackoverflow.com/help/mcve) is always a good debugging step since it helps you identify the root problem. Then, if you can't identify the solution on your own you have a ready-made example to share with others. – dimo414 Jul 03 '17 at 18:46