简体   繁体   English

在NetBeans外部运行时出现空指针异常

[英]Null pointer exception when ran outside netbeans

When I run this bit of code outside of netbeans I get a null pointer exception. 当我在netbeans之外运行这段代码时,我得到一个空指针异常。 I'm trying to read a .DAT file. 我正在尝试读取.DAT文件。 Works fine in netbeans. 在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. 版权所有(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. 该文件位于netbeans上的frogger软件包中。

Your code refers to a src folder which ought to be not present anymore, if your code is deployed. 您的代码引用了一个src文件夹,如果部署了代码,则该文件夹不再存在。 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. 您的IDE通常会将资源从src复制到生成的类文件的位置。 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 如果frogger与您的类的包名称匹配,则可以进行相对查找

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. 请注意,当您捕获IOException时自然会发生NullPointerException,然后继续进行,因为未初始化的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. 但是对于诸如高分之类的事情,您应该考虑使用Preferences API,它为您提供了独立于应用程序位置的持久性存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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