首页 > 解决方案 > Java,看似随机,开始崩溃 FileHandle.class.getResourceAsStream(path);

问题描述

因此,我正在开发一个程序,该程序允许您以 JSON 文件的形式将动画导入 Minecraft,并且在处理程序的完全不同部分时,我的导入代码停止工作。

我正在使用 eclipse,这就是我的导入代码的外观:

package com.github.sam54123.mc_animation.utils;

import java.io.InputStream;

public class FileHandle 
{
    public static InputStream inputStreamFromFile(String path)
    {
        try
        {
            InputStream inputStream = FileHandle.class.getResourceAsStream(path);
            return inputStream;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return null;

    }
}

新文件

package com.github.sam54123.mc_animation.utils;

import java.io.File;
import java.io.InputStream;
import java.util.Scanner;

import org.json.JSONObject;

public class JSONUtils 
{
    public static String getJSONStringFromFile(String path)
    {
        // Open file
        Scanner scanner;
        try 
        {
            InputStream in = FileHandle.inputStreamFromFile(path);
            scanner = new Scanner(in);
            // Get JSON as string without spaces or newlines
            String json = scanner.useDelimiter("\\Z").next();

            // Close file
            scanner.close();

            return json;

        }
        catch(Exception e)
        {
            System.out.println(e.getStackTrace());
            return null;
        }


    }

    public static JSONObject getJSONObjectFromFile(String path)
    {
        File file = new File(path);
        if (!file.exists())
        {
            System.out.println("Invalid Path");
            return null;
        }

        String string = getJSONStringFromFile(path);
        return new JSONObject(string);

    }



}

稍后我会继续对文件进行一些更花哨的呵护。这曾经可靠地工作,直到我在一个完全不同且不相关的类中做到这一点:

String command = getCommand(object);

if (command != null && command.length() > 0)
{
    commands.add(new AnimCommand(command, i));
}

然后它开始抛出这个错误:

   [Ljava.lang.StackTraceElement;@7852e922
Exception in thread "main" java.lang.NullPointerException
    at java.io.StringReader.<init>(Unknown Source)
    at org.json.JSONTokener.<init>(JSONTokener.java:94)
    at org.json.JSONObject.<init>(JSONObject.java:406)
    at com.github.sam54123.mc_animation.utils.JSONUtils.getJSONObjectFromFile(JSONUtils.java:47)
    at com.github.sam54123.mc_animation.system.Animation.<init>(Animation.java:20)
    at com.github.sam54123.mc_animation.testing.Tester.main(Tester.java:13)

我仔细检查了文件没有改变,我尝试删除那段代码,重新启动 Eclipse,整个交易,但似乎没有任何东西可以修复它。该代码甚至能够使用 File 类识别文件是有效的,但似乎没有任何改变。有没有人对如何解决这个问题有一些见解?这是我的其余代码:https ://github.com/Sam54123/mc-animation/

编辑

好的,我刚刚又做了一些调试,看起来是

return new JSONObject(string);

在崩溃的第二个文件的第 47 行。不知道为什么,因为从磁盘读取文件的风险是可以的。

编辑 2 看起来它失败了,因为

InputStream in = FileHandle.inputStreamFromFile(path);

正在返回 null,这是有道理的,因为 try catch 语句

InputStream inputStream = FileHandle.class.getResourceAsStream(path);

是在。为什么这失败了,因为文件的有效性在代码的其他地方得到了验证。它过去也可以工作,而且我没有更改文件布局的任何内容。

编辑 3 有趣的是,一对 System.out.printlns 揭示了 catch 实际上并没有被激活,因此 getResourceAsStream() 实际上必须返回 null。我已经在退货之前打印出来确认了这一点。

标签: javaeclipsefileinputstream

解决方案


推荐阅读