0

I am trying to read a .tab file but getting a NullPointerException error.

Exception in thread "main" java.lang.NullPointerException
    at project.FTAFileReader.<init>(FTAFileReader.java:37)
    at project.FTAFileReader.main(FTAFileReader.java:103)

Please find the code below

public FTAFileReader(){

    try {
            BufferedReader buffer;
            buffer = new BufferedReader(new FileReader("event_trace.tab"));                     
            buffer.readLine(); //Skips the first line of the file. 
            String currentstr;
            while((currentstr=buffer.readLine().trim()) != null){
                String[] column = currentstr.split("\\s+");         

                if(column.length>0){
                    //hostID.add(Long.parseLong(column[0]));
                    eventID.add(Integer.parseInt(column[0]));
                    componentID.add(Integer.parseInt(column[1]));
                    nodeID.add(Integer.parseInt(column[2]));
                    platformID.add(Integer.parseInt(column[3]));
                    nodeName.add(Double.parseDouble(column[4]));
                    eventType.add(Integer.parseInt(column[5]));
                    eventStartTime.add(Long.parseLong(column[6]));
                    eventStopTime.add(Long.parseLong(column[7]));
                    eventEndReason.add(Integer.parseInt(column[8]));                    

                }               

            }
        }
         catch (IOException e) {
            e.printStackTrace();
            System.out.println("The simulation has been terminated due to an IO error");
        }       
}

Any help will be highly appreciable. Thanks in advance.

Yogesh Sharma

  • The result of `String.trim()` can never be `null`; but it's failing because `buffer.readLine()` might be null, so you can't call `buffer.readLine().trim()`. You need to check if `(currentstr=buffer.readLine()) != null` first, and trim it afterwards. – Andy Turner Jan 16 '17 at 12:32
  • Thanks for the help Andy but when I use trim as follows eventID.add(Integer.parseInt(column[0].trim())); then I am getting NumberFormatException –  Jan 16 '17 at 12:44
  • Then you're trying to parse something which isn't a number. And I didn't actually say to do that - I would do `String[] column = currentstr.trim().split(...);`, which is most similar to what you had before. – Andy Turner Jan 16 '17 at 12:51
  • Thanks Andy for your help. This worked well. –  Jan 16 '17 at 14:02

0 Answers0