-1

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
}
RnRoger
  • 682
  • 1
  • 9
  • 33
  • 2
    Obviously, nextLine is null. Do a null check first. Also split on `\\t`. – Roddy of the Frozen Peas Feb 04 '18 at 01:49
  • @RoddyoftheFrozenPeas If I don't try to split it, I can work with nextLine just fine. – RnRoger Feb 04 '18 at 02:18
  • @RnRoger No you can't. It's null. There is no line to work with. The standard way to write this loop is `while ((line = in.readLine()) != null)`. Then you cannot possibly get a `NullPointerException` inside the `while` block. – user207421 Feb 04 '18 at 02:21
  • @RoddyoftheFrozenPeas: What the heck, [the post that you just answered was deleted by the OP](https://stackoverflow.com/questions/48604436/else-if-statements-not-running-through-all-possibilities). I thank you for your efforts, but it looks like the OP just wanted to waste your time. – Hovercraft Full Of Eels Feb 04 '18 at 04:15

1 Answers1

1

This line should be the last one inside your while loop, not the first

nextLine = br.readLine();
Paul MacGuiheen
  • 628
  • 5
  • 17