0

When I run this bit of code outside of netbeans I get a null pointer exception. I'm trying to read a .DAT file. Works fine in netbeans. I've checked the path many times and it is definitely right. Is a class loader or something like that supposed to be used?

static String fileName = "src/frogger/highScores.DAT";
try {
        //Make fileReader object to read the file
        file = new FileReader(new File(fileName));
        fileStream = new BufferedReader(file);

    } catch (Exception e) {
        System.out.println("File not found");
    }

I was imagining something being used like this with images, but it doesn't work.

 file = new FileReader(new File(this.getClass().getResource(fileName)));

or

file = new FileReader(this.getClass().getResource(fileName));

Error

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\Michael>java -jar "C:\Users\Michael\Documents\NetBeansProjects\Frogger\
dist\Frogger.jar"
File not found
Exception in thread "main" java.lang.NullPointerException
        at frogger.Board.readFile(Board.java:519)
    at frogger.Board.gameInit(Board.java:154)
    at frogger.Board.addNotify(Board.java:111)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at javax.swing.JRootPane.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at java.awt.Window.addNotify(Unknown Source)
    at java.awt.Frame.addNotify(Unknown Source)
    at java.awt.Window.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at frogger.Window.<init>(Window.java:16)
    at frogger.Window.main(Window.java:22)

The file is in the package frogger on netbeans.

Michael Haywood
  • 319
  • 2
  • 4
  • 10
  • Can you post your stack trace? – predi Aug 28 '13 at 09:53
  • 1
    When does the exception occur? Besides, the path is relative, thus depending on your working directory. So running your application outside of NetBeans will most likely result in a different working directory. So either use the same working directory or use an absolute path. – bcause Aug 28 '13 at 09:53
  • Try to use : `System.getProperty("user.dir")` to get the current folder of your application so it'll be like : `File f = new File(System.getProperty("user.dir") + "/frogger/...")` – Sw4Tish Aug 28 '13 at 10:16
  • Is your dat file intended to be read only? It's name suggests otherwise. – predi Aug 28 '13 at 10:28
  • That doesn't seem to work i'm afraid. – Michael Haywood Aug 28 '13 at 10:30
  • It gets written to aswell. I'm using it to get the highscores for my game, then I write the new highscores to it aswell. – Michael Haywood Aug 28 '13 at 10:34
  • Then reading [this](http://stackoverflow.com/a/14380146/878469) might help you. – predi Aug 28 '13 at 10:37

1 Answers1

0

Your code refers to a src folder which ought to be not present anymore, if your code is deployed. The right way to access resources would be

// no src/ in the resource name
InputStream is=getClass().getResourceAsStream("/frogger/highScores.DAT");
fileStream = new BufferedReader(new InputStreamReader(file));

Your IDE typically copies resources from src to the location of the generated class files. But if you deploy your application you have to care that the file is packaged with it.

If frogger matches the package name of your class you can do a relative lookup

InputStream is=getClass().getResourceAsStream("highScores.DAT");

Note that the NullPointerException comes naturally as you are catching the IOException and then proceeding as nothing has happened with an uninitialized Reader. You should rather ensure the code dealing with the resource is not executed when the allocation fails.

But for things like highscores you should consider the Preferences API instead which gives you a persistent storage independent from your application’s location.

Holger
  • 285,553
  • 42
  • 434
  • 765