简体   繁体   中英

Null pointer exception when ran outside netbeans

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM