Problems passing a scan object to a method
Hello Everyone, A special hello to advanced java users and moderators.
I am currently doing a very intensive java course and have a problem with pass in Scanner object to a method in a class.
In the main method i have the following codes:-
public static void main(String args) throws IOException
Scanner scan = new Scanner(new File("magicData.txt"));
int count = 1; //count which square we're on
int size = scan.nextInt(); //size of next square
//Expecting -1 at bottom of input file
while (size != -1)
Square square = new Square(size);
//create a new Square of the given size
I am having a problem with the last line.
it gives the error
Exception in thread "main" java.lang.NullPointerException
Here is the method in the Square class
public void readSquare(Scanner scan)
for (int row = 0; row < square.length; row++)
for (int col = 0; col < square.length; col ++)
square[row][col] = scan.nextInt();
What can i do to fix this problem and prevent problems like these from occuring in the future?