Help with reading from file
I am currently making a program to read each char in a text file and adding it into an array.
The problem is that it can't find the file. I have tried to print out the absolute path and the path is correct.
I can't see what I'm going to change.
Here's my code:
Code java:
package trust.src;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Level {
private int level;
private char[][] tiles;
private String[] lines;
public Level() {
tiles = new char[20][30];
level = 1;
getLevel();
}
public void getLevel() {
String fileName = "level" + Integer.toString(level) + ".txt";
try {
//creates the file.
File file = new File("res/levels/" + fileName);
String line;
int index = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
//reads the lines and add them into a String array.
while ((line = reader.readLine()) != null) {
lines[index] = line;
index++;
}
//reads each char in each line.
for (int i = 0; i < lines.length; i++) {
for (int j = 0; j < lines[i].length(); j++) {
tiles[i][j] = lines[i].charAt(j);
System.out.println(lines[i]);
}
}
}catch(Exception ex) {System.out.println("Error while loading level"); ex.printStackTrace();}
}
}
and this is the output i get:
Code :
Error while loading level
java.lang.NullPointerException
at trust.src.Level.getLevel(Level.java:30)
at trust.src.Level.<init>(Level.java:17)
at trust.src.Board.<init>(Board.java:8)
at trust.src.Trust.<init>(Trust.java:13)
at trust.src.Trust.main(Trust.java:18)
I dont know what i should do.
Please help me!
Re: Help with reading from file
Quote:
java.lang.NullPointerException
at trust.src.Level.getLevel(Level.java:30)
There is a variable on line 30 with a null value. Look at line 30, find the variable and then backtrack in the code to find out why the variable does not have a valid value.
Re: Help with reading from file
Omg, this is crazy. I backtracked the error and fixed it, but when I print out each line in the line array.
I get, instead of 20 lines, 599 lines! I have no idea why.
here is my text file(I know it looks wierd, but there are 20 rows, and 30 chars on each row):
Code :
##############################
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#_______________________OC___#
##############################
##############################
And here is the 599 rows output I get:
Code :
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#____________________________#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
#_______________________OC___#
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
This is really wierd, almost humoristic, but It's also REALLY annoying.
I have no idea why this happens.
Can somebody help me!
Re: Help with reading from file
Post the new code with the problem.
Re: Help with reading from file
Right, here's the code:
Code java:
package trust.src;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Level {
private int level;
private char[][] tiles;
private String[] lines;
public Level() {
lines = new String[20];
tiles = new char[20][30];
level = 1;
getLevel();
}
public void getLevel() {
String fileName = "level" + Integer.toString(level) + ".txt";
try {
File file = new File("res/levels/" + fileName);
System.out.println(file.getAbsolutePath());
String line;
int index = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
lines[index] = line;
index++;
}
for (int i = 0; i < lines.length; i++) {
for (int j = 0; j < lines[i].length(); j++) {
tiles[i][j] = lines[i].charAt(j);
System.out.println(lines[i]);
}
}
}catch(Exception ex) {System.out.println("Error while loading level"); ex.printStackTrace();}
}
}
Re: Help with reading from file
There is a nested loop where the println is located. How many times will the println() method be called inside those loops?
Multiply it out to get the total number of times the println() method is called.
Is that the number of lines that you want to print?
Re: Help with reading from file
LOL! I'm so stupid...
Thx man!