0

I have made a main class as well to run it, but it is throwing out an NPE NullPointerException at the readFile() method. I'm not sure why this is occurring.

/* this code is to read from a file testfile.txt which is already made beforehand with data in it*/
import java.io.*; //imported files
import java.util.*;

public class file_read {
  private Scanner x; //for reading from file

  public void openFile() {
    try {
      x = new Scanner(new File("testfile.txt"));
    } catch (Exception e) {
      System.out.println("error occurred");
    }
  }

  public void readFile() { //error in this method(NPE)
    while (x.hasNext()) {
      String a = x.next();
      String b = x.next();
      String c = x.next();
      System.out.printf("%s %s %s \n", a, b, c);
    }
  }

  public void closeFile() {
    x.close();
  }
}

public class file_read_main {
  public static void main(String[] args) {
    file_read obj = new file_read();
    obj.openFile();
    obj.readFile();
    obj.closeFile();
  }
}

This is one class. The other class is already made having main() in it having object of this class as well as calling of the methods from the object.

metasim
  • 4,793
  • 3
  • 46
  • 70
  • 2
    I'm going to guess that either you're not calling `openFile()`, or you're ignoring an exception thrown in that method. But this is a guess, as you've not shown how you call methods on this class. Please add that code. – Andy Turner Jun 05 '16 at 22:24
  • public class file_read_main { public static void main(String[] args) { file_read obj= new file_read(); obj.openFile(); obj.readFile(); obj.closeFile(); } } – Abhi Gambhir Jun 05 '16 at 22:26
  • 1) You don't post the exact line that causes the NPE. 2) You're doing an awful lot of `x.next()` after checking `x.hasNext()` only once. You should match any next with a hasNext. – Hovercraft Full Of Eels Jun 05 '16 at 22:26
  • this my main so definitely i called up openFile(), i dont knw wat to do? – Abhi Gambhir Jun 05 '16 at 22:27
  • On the whole, it is a bad idea to swallow exceptions like that (or even to catch `Exception` when you probably only need catch `IOException`). Add `e.printStackTrace()` to the catch block, to see what the actual problem is. – Andy Turner Jun 05 '16 at 22:27
  • @HovercraftFullOfEels can u write the piece of code n explain? – Abhi Gambhir Jun 05 '16 at 22:29
  • I was wrong. Read @AndyTurner's answer as he's likely got the issue in hand. 1+ – Hovercraft Full Of Eels Jun 05 '16 at 22:31
  • @HovercraftFullOfEels thats the constructor common its file_read() n not the read method that is giving me error. – Abhi Gambhir Jun 05 '16 at 22:31
  • @HovercraftFullOfEels you're not wrong that the three `next()`s are a potential problem... just not the cause of an NPE. – Andy Turner Jun 05 '16 at 22:32
  • ya sure let me try that @HovercraftFullOfEels thanks to you both – Abhi Gambhir Jun 05 '16 at 22:33

1 Answers1

2

The problem is that x is null.

Based on the code that you have shown, this can only be because an exception is being thrown in openFile(), and you are ignoring it.

Make openFile throw its exception:

public void openFile() throws IOException {
  x = new Scanner(new File("testfile.txt"));
}

(you will need to add throws IOException to your main method too)

Now the fact that an IOException has been thrown will stop your code executing, so it won't try to read the file.

Assuming you've got no default exception handler set up, you will also get the stack trace of the exception, which will include details of why the exception was thrown.


As a general principle about exception handling, don't catch Exception, unless that's really the exception that you really need to handle. It catches all exceptions, and might accidentally catch certain exception types which really should be handled separately.

You should also not just swallow exceptions unless you are really sure that is the right thing to do. The bare minimum you should do with an exception is to print its stack trace in the catch:

e.printStackTrace();

This is not the best choice in the current situtation, however, since you actually need to stop further execution in the calling method: propagating the exception is a better choice.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243