I've been getting null pointer exception while reading a file into an array. i realized that the exception appears when it is null and something else is required. The array minefield has already been initialized. The exception happens on " minefield[i][j]=input.charAt(j)+"";"
I'm trying to read in a file with this format:
#of row
#of column
abcd
efgh
ijkl
This is the code:
try {
BufferedReader in =new BufferedReader (new FileReader(name+".txt"));
String input=in.readLine();
row = Integer.parseInt(input);
input=in.readLine();
col = Integer.parseInt(input);
int c =0;
input=in.readLine();
for (int i=0;i<row;i++){
input=in.readLine();
for (int j=0;j<col;j++){
System.out.println (input.charAt(j));
minefield[i][j]=input.charAt(j)+"";
}
}
System.out.println("The file has been loaded");
in.close();
}
catch(IOException iox){
System.out.println ("Error reading file");
}
Your help is greatly appreciated. Edit: Sorry i left something out.
row? Are thererowlines in the file?