0

I am getting input from a file. The file has two columns. I want to store each column in an array. This is what i'm doing.

   String strArr;

    for(int x=0;x<=m;x++){  // m is the number of lines to parse

      strArr = bufReader.readLine(); 
     String[] vals=strArr.split("\\s+ ");
      System.out.println(strArr);

    nodei[x]=Integer.parseInt(vals[0]);
    nodej[x]=Integer.parseInt(vals[1]);

      }

I encounter NullPointerException at

String[] vals=strArr.split("\\s+ ");

How do I resolve?

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
shri
  • 37
  • 5
  • Your question may already have an answer here: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Pokechu22 Mar 29 '15 at 19:44

1 Answers1

1

if strArr is null, and you call .split on it, it will throw a null pointer. You can check if null before using .split.

String strArr;

for(int x=0;x<=m;x++){  // m is the number of lines to parse

  strArr = bufReader.readLine(); 
  if (strArr != null) {
      String[] vals=strArr.split("\\s+ ");
      System.out.println(strArr);

      nodei[x]=Integer.parseInt(vals[0]);
      nodej[x]=Integer.parseInt(vals[1]);
  }
}
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29