NullPointer with BufferedReader and split() [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am tring to read a file which has lines like so:
438782 Abaca bunchy top virus NC_010314 Musa Sp.
So the lines contain information seperated by tabs. I am tring to read this file and do something with every line after splitting them. It keeps throwing a NullPointerException error though. This always happens on the line where I try to split. In the code below I left everything unrelated to this issue out.
BufferedReader br = new BufferedReader(new FileReader(filename));
String nextLine = br.readLine();
String[] line;
while (nextLine != null) {
nextLine = br.readLine();
line = nextLine.split("\t"); //Error line
//Do something with line
}

This line should be the last one inside your while loop, not the first
nextLine = br.readLine();

Related

Why this code skip the first line of file? [duplicate]

This question already has answers here:
Reading UTF-8 - BOM marker
(9 answers)
Closed 5 years ago.
I applied this code more than one, this code is to read a file and for each line, it should create a new object and add it to att_agreement ArrayList, it works fine for each line except the first line, I can not find its object in the output.
Any help, please?
public ArrayList<Att_Elements> load_ann(File f) {
ArrayList<Att_Elements> att_agreement = new ArrayList<Att_Elements>();
String line="";
try {
BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF8"));
while((line = read.readLine()) != null) {
String[] SplitLine = line.split("\\|");
if (SplitLine[0].equals("Att")) {
annotation=new Att_Elements();
annotation.Type = SplitLine[0];
.
.
.
//...
att_agreement.add(annotation);
}
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
return att_agreement;
}
Here is a sample of file content (3 lines):
Your file likely has what is called a BOM located at the beginning. This is a byte order mark. Thus, your conditional .equals("Att") is not being met until the second line where the BOM is not present. A separate if statement to handle this case should work well. If you print each line read, you should see what the BufferedReader is reading as the first line. The new conditional statement can then be tailored to this value.
Another approach is to search for a generic BOM string and replace it with nothing.

How do I read only one line of text file? [duplicate]

This question already has answers here:
How to read a specific line using the specific line number from a file in Java?
(19 answers)
Closed 5 years ago.
My program is reading all lines in the file but i just need the second one.
String line;
try (
InputStream fis = new FileInputStream(source);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr)) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.
br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output

How can i read text file and store text values in data base in java? [duplicate]

This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 5 years ago.
I have text file which i have read once user upload, how can i read using java code, can any one give some suggestion which would very helpful to me
sample file looks like below
SNR Name
1 AAR
2 BAT
3 VWE
A common pattern is to use
try (BufferedReader br = new BufferedReader(new FileReader(file)) ) {
String line;
while ((line = br.readLine()) != null) {
// process the line to insert in database.
}}
The easiest way is use Scanner() object
Scanner sc = new Scanner(new File("myFile.txt"));
use
boolean hasNext = sc.hasNext();
to know if there are more items in the file
and
String item = sc.next();
to get items secuentially.
I attach the documentation (it provides very good code examples)
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You can use BufferedReader to read file line by line.
So, even if your file is too big, then also it will read line by line only. It won't load entire file. Declaration will be similar to this.
BufferedReader br = new BufferedReader(new FileReader(FILENAME));
And
you can use command
while ((sCurrentLine = br.readLine()) != null) { .....// process
}
to read file till end.
Obviously, you have to use seperator in file to split the records in each line. And store accordingly in java objects.
After that you can store in DB through DAO.

How does bufferedreader readline work? [duplicate]

This question already has answers here:
Using BufferedReader.readLine() in a while loop properly
(7 answers)
Closed 7 years ago.
if you have something like this
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
Why does bufferedeader.readline() read the next line after the first one? What's confusing to me is that there isn't a readnextline method and I don't understand why readline would continue reading the rest of the file instead of looping the first line infinitely.
You can rewrite this to:
line = bufferedReader.readLine()
while (line != null) {
... print ...
line = bufferedReader.readLine();
That should answer you question ...
(the point being the fact that readLine(); well, reads ONE line; after the other; and returns null if there wasnt any more line to read)

NullPointerException when storing lines in an arraylist [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm getting a NullPointerException in java (at this line :FileReader fstream = new FileReader(fileName)) when I try to read a text file line by line and store them in an arraylist with this code :
public ArrayList<String> StoreLineByLine(String fileName) throws IOException {
String str;
ArrayList<String> Line = new ArrayList<String>();
FileReader fstream = new FileReader(fileName);
BufferedReader myFileReader = new BufferedReader(fstream);
while ((str = myFileReader.readLine()) != null) {
Line.add(str);
}
myFileReader.close();
return Line;
}
Could anyone help me understand the problem ?
Thanks a lot !
If you're getting a NPE on that line, then fileName must be null.
BTW, if you're using JDK 8 then this may be a better way to load the lines. Replace the contents of your method with this:
return Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
...and change the method's return type from ArrayList<String> to List<String>.

Categories

Resources