0

I have a piece of code in Java Swing which browses a txt file and then prints the content of the file in a text area. This is the method which does the work. For some reason which I am overlooking most probably, I am getting a null pointer exception every time I am testing the code. Can you please tell me what I have done wrong or maybe provide a link?

private void showText() {                
    try{
        //filePathInputField.setText(new File(file.getFile()).getAbsolutePath());
        FileReader fr = new FileReader(fileInputPathField.getText());
        BufferedReader br = new BufferedReader(fr);
        while((sourceText = br.readLine()) != null){
            sourceText += br.readLine();
        }
        sourceTextArea.setText(sourceText);
    } catch (Exception ex){
        showMessage();
        ex.printStackTrace();
    }
}

The Error that i received after changing the code slightly from your suggestions are:

java.io.FileNotFoundException: D:\Java\GUI Project Files\Crypto\Audio Specs.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at java.io.FileReader.<init>(FileReader.java:58)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CrazyCoder
  • 15
  • 5
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Always copy/paste error and exception output! 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Mar 01 '15 at 05:21
  • @AndrewThompson, I have seen them but still i couldnt fix the error. Must be some mistake I made during the coding part. The stack traces are given below. Can you please check and see? – CrazyCoder Mar 01 '15 at 05:48
  • I don't try to read code or stack traces in comments, instead [edit the question](http://stackoverflow.com/posts/28790208/edit). *Where is the MCVE?* – Andrew Thompson Mar 01 '15 at 05:51
  • I think the `FileNotFoundException` is rather obvious: the program cannot find the file. Check the path you provided in the textfield. – osechet Mar 01 '15 at 07:13

1 Answers1

0

getClass().getResource() is for packaged files (aka resources). If filePathInputField contains a path to a file in your filesystem, that call will return null. You can access it using an instance of File or Path.

duckstep
  • 1,148
  • 8
  • 17
  • Thank you for the input but I have earlier tried it with File but that gave the same result. So i did a little reading from here and tried URL. Any other ideas why it might be happening? – CrazyCoder Mar 01 '15 at 04:57
  • earlier i was using this code module in the URL's place: ` sourceTextArea.setText(new File(file.getFile()).getAbsolutePath()); ` – CrazyCoder Mar 01 '15 at 04:58
  • Sorry the earlier code was: earlier i was using this code module in the URL's place: ` FileReader fr = new FileReader(new File(file.getFile()).getAbsolutePath()); ` – CrazyCoder Mar 01 '15 at 05:05
  • In that case, `new FileReader(new File(filePathInputField.getText()))` should work. If it doesn't, give more info (i.e. full stacktrace of your NPE). – duckstep Mar 01 '15 at 05:07
  • This is the error I am getting from the stacktrace: java.io.FileNotFoundException: Audio Specs.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:97) at java.io.FileReader.(FileReader.java:58) – CrazyCoder Mar 01 '15 at 05:12
  • `Audio Specs.txt` is not an absolute path. See where the `File` points to like this: `System.out.println(new File("Audio Specs.txt").getAbsolutePath());` – duckstep Mar 01 '15 at 05:21
  • After having changed it to absolute path, I am still getting the same error. java.io.FileNotFoundException: D:\Java\GUI Project Files\Crypto\Audio Specs.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:97) at java.io.FileReader.(FileReader.java:58) – CrazyCoder Mar 01 '15 at 05:44
  • Does that file exists? Can you access it? `File f = new File("D:\\Java\\GUI Project Files\\Crypto\\Audio Specs.txt"); System.out.println(f.exists()); System.out.println(f.isFile()); System.out.println(f.canRead()); System.out.println(f.canWrite());` – duckstep Mar 01 '15 at 05:53
  • yes i can access it anytime. Its just on a different directory. I dunno why the swing cannot read it. – CrazyCoder Mar 01 '15 at 05:56
  • the thing is, I am using a Swing file choser to let the user get the file. So, I cannot really edit the path with the '\\'. Any way to go around this? – CrazyCoder Mar 01 '15 at 06:00
  • Swing has nothing to do with your file reading code. Use the full path in your input field, or (better yet) use a [`JFileChooser`](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html) and get the File instance by `getSelectedFile()`. – duckstep Mar 01 '15 at 06:02