-3

getting a frustrating null exception error when the class junction is being used in functions. The class should be initialised so this shouldn't be an issue. Help ;_;

class RobotData {
class Junction {
    public int juncX;
    public int juncY;
    public int arrivalHeading;
}

private static int maxJunctions = 10000; //Max junctions likely to occur
private static int junctionCounter;
private static Junction[] junction;

RobotData() {
    junctionCounter = 0;
    junction = new Junction[maxJunctions];
}

public void resetJunctionCounter() { junctionCounter = 0; }

public void recordJunction(IRobot robot) {
    junction[junctionCounter].juncX = robot.getLocation().x;
    junction[junctionCounter].juncY = robot.getLocation().y;
    junction[junctionCounter].arrivalHeading = robot.getHeading();
    junctionCounter++;
}

public void  printJunction() {
    System.out.println(junction[junctionCounter].juncX);
    System.out.println(junction[junctionCounter].juncY);
    System.out.println(junction[junctionCounter].arrivalHeading);
}
}

The class RobotData is being initialised properly, but when the functions are being called I get the null error indicating that junction[junctionCounter] hasn't been initialised yet. Unsure why (obviously) as it should be initialised when RobotData is.

1 Answers1

3

When you write this line:

junction = new Junction[maxJunctions];

You create an array of maxJunctions references to Junction instances on the heap. All of them are null until you point them to an object on the heap by calling new.

Here's another idea:

public class Junction {
    public final int juncX;
    public final int juncY;
    public final int arrivalHeading;

    public String toString() {
        return String.format("x = %d y = %d arrivalHeader = %d", juncX, juncY, arrivalHeading);
    }
}

public class RobotData {

    private List<Junction> junctions;

    RobotData() {
        this.junctions = new ArrayList<Junction>();
    }

    public void recordJunction(IRobot robot) {
        Junction junction = new Junction();    
        junction.juncX = robot.getLocation().x;
        junction.juncY = robot.getLocation().y;
        junction.arrivalHeading = robot.getHeading();
        junctions.add(junction);
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I don't quite understand fully. So because this is an array, it's declaring the array, but not initialising each individual element? Is there a way to do this without looping? – LozzaManiac Nov 09 '14 at 19:28
  • 1
    Please read an intro Java text on arrays. This should have been covered. I'm curious: what do you think Junctions should be initialized to? How should that be done? – duffymo Nov 09 '14 at 19:32
  • Sorry been trying to read up, from what I can find on the internet such as [here](http://www.tutorialspoint.com/java/java_arrays.htm) oops didn't mean to post hang on I want to effectively go junction[0].juncX and get the first recorded junction juncX variable and set it to something. I'm pretty sure I've done this in VB but I'm completely new to java. From what you've done here, I get the idea that you can't initialise each array element at once, you have to do it manually? – LozzaManiac Nov 09 '14 at 19:39
  • Actually why would I need to do them all at once, I could just do it as I make a new one, as you did... hmm – LozzaManiac Nov 09 '14 at 19:42
  • You haven't recorded a Junction instance until you do what I did. Looks better. – duffymo Nov 09 '14 at 19:45
  • I'd start going further: Why doesn't your IRobot interface return a Junction instead of all those primitives? Why doesn't a Robot keep its own data? – duffymo Nov 09 '14 at 19:45
  • Got it using your method thanks a bunch! (Sorry it took a while, I wanted to make sure I understood how it worked, but after a while it clicked it was just a linked list) EDIT - oh that's smart... I'll look into it ! – LozzaManiac Nov 09 '14 at 20:05