Java OOP Help - Saddle Point

Associate
Joined
3 Oct 2006
Posts
2,304
Location
London
Before I get into the problem I'd like to state that I am NOT looking for someone to complete my assignment for me, I'm just after a little bit of guidance.

I'm stuck with some OOP in java. It's my first ever class/package in java and it all compiles but running it doesn't go so well:

Exception.png


Basically the challenge is to generate a grid, of X size, display it and then work out where the saddle point is. The problem I am having though, at the moment, is generating the grid.

Source Code : http://www.imgholder.co.uk/files/1/Assignment6.zip

There is no stipulation that this had to be written with a class, I just wanted to challenge myself, so if worst comes to worst I can revert back to a procedural version.

Does anyone have any ideas as to why I am getting the error in above screenshot?
 
NullPointerException indicates you are trying to make a null value do something. The below example would compile, but throw a null pointer exception when run.
Code:
String variable;
variable.indexOf('x'); //String is still null at this point
Also, as an FYI/Tip, when looking at the stack trace (which is what you have taken a screeny of) only ever worry about the top most error before fixing others.
 
From the stack trace we can see that the NullPointerException was raised on line 47 of Grid.java in the method createGrid.

That line is:
Code:
grid[row][col] = generator.nextInt(10);
row and column look fine, no idea what generator is so I'll check "grid" first.

This is declared at the top of your class as:
Code:
private int grid[][];

I can't see anywhere in the class where you instantiate the object before using it.

I can't remember the Java syntax but is something like:
Code:
grid = New int[gridSize][gridSize];

You get the idea :)
 
Back
Top Bottom