Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileInputStream NullpointerException_Java_Java Io - Fatal编程技术网

Java FileInputStream NullpointerException

Java FileInputStream NullpointerException,java,java-io,Java,Java Io,我正在尝试读取属性文件/res/version.num。 但我得到的只是一个NPE try { URL url = getClass().getResource("/res/version.num"); logger.writeLog(JammEnum.INFO, "version.num path: " + url.getPath()); File prop_file = new File(url.getPath()); FileInputStream fis

我正在尝试读取属性文件
/res/version.num
。 但我得到的只是一个NPE

try {
    URL url = getClass().getResource("/res/version.num");
    logger.writeLog(JammEnum.INFO, "version.num path: " + url.getPath());
    File prop_file = new File(url.getPath());
    FileInputStream fis = new FileInputStream(prop_file);
    prop.load(fis);
    fis.close();
} 
catch (FileNotFoundException e) {
    logger.writeLog(JammEnum.ERROR, "File not found: version.num");
    e.printStackTrace();
} 
catch (IOException e) {
    logger.writeLog(JammEnum.ERROR, "Cannot read version.num");
    e.printStackTrace();
}
输出为:

(II) - version.num path: /home/andreas/Programmieren/Java/Jamm/bin/res/version.num
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at jamm.gui.InfoGui.<init>(InfoGui.java:83)
    at jamm.gui.FinanzmanagerGui$ButtonInfoListener.actionPerformed(FinanzmanagerGui.java:553)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
(II)-version.num路径:/home/andreas/Programmieren/Java/Jamm/bin/res/version.num
线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常
位于jamm.gui.InfoGui(InfoGui.java:83)
在jamm.gui.FinanzmanagerGui$ButtonInfoListener.actionPerformed处(FinanzmanagerGui.java:553)
在javax.swing.AbstractButton.fireActionPerformed上(AbstractButton.java:2018)
位于javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
在javax.swing.DefaultButtonModel.fireActionPerformed上(DefaultButtonModel.java:402)
在javax.swing.DefaultButtonModel.setPressed中(DefaultButtonModel.java:259)
在javax.swing.plaf.basic.BasicButtonListener.MouseRelease(BasicButtonListener.java:252)
位于java.awt.Component.ProcessMouseeEvent(Component.java:6504)
位于javax.swing.JComponent.ProcessMouseeEvent(JComponent.java:3321)
位于java.awt.Component.processEvent(Component.java:6269)
位于java.awt.Container.processEvent(Container.java:2229)
位于java.awt.Component.dispatchEventImpl(Component.java:4860)
位于java.awt.Container.dispatchEventImpl(Container.java:2287)
位于java.awt.Component.dispatchEvent(Component.java:4686)
位于java.awt.LightweightDispatcher.RetargetMouseeEvent(Container.java:4832)
位于java.awt.LightweightDispatcher.ProcessMouseeEvent(Container.java:4492)
位于java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
位于java.awt.Container.dispatchEventImpl(Container.java:2273)
位于java.awt.Window.dispatchEventImpl(Window.java:2713)
位于java.awt.Component.dispatchEvent(Component.java:4686)
位于java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
在java.awt.EventQueue.access$000(EventQueue.java:101)
在java.awt.EventQueue$3.run(EventQueue.java:666)
在java.awt.EventQueue$3.run(EventQueue.java:664)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
在java.awt.EventQueue$4.run(EventQueue.java:680)
在java.awt.EventQueue$4.run(EventQueue.java:678)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
位于java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
位于java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread.java:128)
位于java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThread.java:117)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
第83行是:FileInputStream fis=新的FileInputStream(prop_文件)

文件的路径记录正确。但为什么会出现此异常?

您需要使用,而不是FileInputStream。

试试这个例子

$ cat src/PrintVersion.java 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author peter.lawrey
 */
public class PrintVersion {
    public static void main(String... args) throws IOException {
        InputStream is = PrintVersion.class.getClassLoader().getResourceAsStream("res/version.num");
        String version = asString(is);
        System.out.println("Version: "+version);
    }

    public static String asString(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        for(int len; (len = is.read(bytes))>0;)
            baos.write(bytes, 0, len);
        return baos.toString();
    }
}
$ cat src/res/version.num 
version 1.0
$ javac src/PrintVersion.java
$ ls src
PrintVersion.class  PrintVersion.java  res
$ java -cp src PrintVersion
Version: version 1.0

url
为空,因为它找不到资源。即使它不为null,这也不是获取文件的正确方法,因为只有当文件位于文件系统上时,它才会工作

你应该试试

InputStream is = getClass().getClassLoader()
                 .getResourceAsInputStream("/res/version.num");

谢谢你的帮助。但我仍然得到这个NPE。这条路对吗?对于所有这些问题,我很抱歉,我对编码很陌生。目前我的文件位于“Project”/src/res/version.num中。您给出的路径必须是相对于类路径的。e、 g.如果
target/classes/
在您的类中,则路径需要位于名为res.的子目录中,即
target/classes/res/version.num
PAH。我不能让它工作。我的文件夹结构是,这是我的.classpath和当前代码,我怀疑您需要从路径中删除前导“/”。这在Tomcat中很有效,但在普通Java中可能不起作用。看我的例子。谢谢,谢谢,谢谢,。。。这么愚蠢的错误!我搜索了几个小时:(1000倍再次感谢