【发布时间】:2016-12-21 16:37:09
【问题描述】:
我正在尝试创建一个插件,用于存储一些 Minecraft 项目的数据以及一些属性。
这是我的 YAML 文件的内容:
rates:
- 391:
mul: 10000
store: 5000
- 392:
mul: 9000
store: 5000
所以它基本上是一个地图列表(至少我是这么认为的)。 这是我试图访问 '391' 的键 'mul' 的 JAVA 代码:
List<Map<?,?>> rates;
rates= getConfig().getMapList("rates");
for(Map<?,?> mp : rates){
Map <?,?> test = (Map<?,?>) mp.get("" + item);
player.sendMessage(test.toString());// HERE I get null pointer exception, and the following lines if this line wasn't there in the first place
player.sendMessage("Mul is: " + test.get("mul"));
player.sendMessage("Store is: " + test.get("store"));
}
根据建议的答案,这是我的测试代码,我仍然得到 NullPointerException:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;
import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;
public class Test {
public static void main(String[] args) throws FileNotFoundException, YamlException{
YamlReader reader = new YamlReader(new FileReader("config.yml"));
Map map = (Map) reader.read();
Map itemMap = (Map) map.get("391");
System.out.println(itemMap.get("mul"));//This is where I get the exception now
System.out.println(itemMap.get("store"));
}
}
【问题讨论】:
-
@pvg 我知道 NullPointerException 是什么,我只是不知道为什么我的代码会发生这种情况。我想这与 yaml 解析有关。
-
这仍然是一个骗局,因为答案是关于如何诊断 NPE 的原因 - 这样做,你几乎肯定会解决你的问题。您也没有提供您得到的特定异常,也没有提供其他人可以尝试的代码 - 请查看有关如何编写 minimal reproducible example 的建议
-
@pvg 关于 NullPointerException,我从一个小时以来一直在努力解决这个问题,但无法做到,这就是我在此处发布的原因。我不同意这不是重复的,但我也不同意它是。我正在寻找针对更具体问题的具体解决方案。 MVC 部分,对此感到抱歉。我对在这里找到一个 bukkit 程序员抱有很高的期望,我只是专注于更多地减少代码(这是错误的,我接受)。
-
花费了多长时间并不重要,重要的是问题是什么,之前是否已经回答过,以及它如何帮助其他人。您仍然没有包含实际的异常堆栈跟踪。
标签: java yaml minecraft bukkit