当我在 netbeans 之外运行这段代码时,我得到一个空指针异常。我正在尝试读取 .DAT 文件。在netbeans中工作正常。我已经检查了很多次路径,它绝对是正确的。是否应该使用类加载器或类似的东西?
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");
}
我在想像这样与图像一起使用的东西,但它不起作用。
file = new FileReader(new File(this.getClass().getResource(fileName)));
或者
file = new FileReader(this.getClass().getResource(fileName));
错误
Microsoft Windows [Version 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。版权所有。
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)
该文件位于 netbeans 上的 frogger 包中。