java - 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。我已经在退货之前打印出来确认了这一点。
解决方案
推荐阅读
- python - 使用 python (Raspbery Pi) 在 excel 中记录信息
- robotics - pcl::visualization c++ api 是否支持用点云显示 2D 图像?
- javascript - 为什么此文件注册脚本中未定义“交互”参数?
- python - 线性回归函数试图为 LASSO 模型找到最佳 lambda 值.. 输出错误
- c++ - 尝试使用 c++ 库时出现奇怪的错误
- c# - 如何在 .Net/C# 中检测 Websocket 断开连接?
- google-sheets - 如何使用多行进行查找
- javascript - 如何将查询与 MongoDB 运算符结合起来?
- reactjs - 如何限制自动完成输入 / ant.design 的结果数量
- python - 主函数使变量未定义?