I am trying to fill an array of chars using data input from file, while parsing the file I am also trying to store some values, but things are not working.
This is my code:
File file = new File(filename); // filename is a user input
Scanner fileScan = new Scanner(file);
// determine the board size
ROWS = fileScan.nextInt();
COLS = fileScan.nextInt();
fileScan.nextLine();
char[][] board = new char[ROWS][COLS];
for( int row = 0; row < board.length; row++){
for (int col = 0; col < board[row].length ; col++){
if (fileScan.hasNext()) {
board[row][col]= fileScan.next().charAt(0);
if(board[row][col] == START){
startingPoint = new Point(row, col);
}
if(board[row][col] == END){
endingPoint = new Point(row, col);
}
}
}
}
}
Given that then input file has
2 2
1 O O
O O O
O X 2
this is what i get when I run it (when I try to print the array):
O O
O X
java.lang.NullPointerException
at java.awt.Point.<init>(Unknown Source)
at CircuitBoard.getStartingPoint(CircuitBoard.java:139)
at CircuitTracer.<init>(CircuitTracer.java:120)
at CircuitTracer.main(CircuitTracer.java:36)
What is going wrong that prevents me from getting a value for the startingPoint ?
well, i have tried to run the code: taking in consideration my comment about data size 2x2 --> 3x3
it worked OK
the file should be
3 3
1 O O
O O O
O X 2
reading code is fine, and this is the array printing code i used
for( int row = 0; row < board.length; row++){
for (int col = 0; col < board[row].length ; col++){
System.out.print(board[row][col] + " ");
}
System.out.println("");
}
this was the output:
1 O O
O O O
O X 2
so if this is what you need, just change 1st line in file to 3 3
and use the print code in this answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.