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.