Hashmap 崩溃 Eclipse

发布于 2024-12-07 22:10:18 字数 614 浏览 0 评论 0原文

我试图通过解析 Android 中的 plist 文件来加载哈希图,使用这里的 plist 解析器: https:// github.com/ZhouWeikuan/cocos2d。 这在过去工作得很好,但最近我的程序在使用这个哈希图时(解析后)出现了空指针异常。 当我尝试调试时,Eclipse 开始表现得很奇怪。解析器返回哈希图,我可以在变量视图中查看它的值。当我跨过分配给“worldMap”的线时,Eclipse 挂起。当尝试在调试模式下查看 worldMap 的值时,我可以看到一个空行但没有值 - 最终 eclipse 崩溃。

Map worlds = (Map)getWorlds().get("Worlds");
Map worldMap = (Map)worlds.get(String.valueOf(world));
Map levels = (Map)worldMap.get("Levels");

然而,当正常运行程序时,空指针异常会在这些行之后出现。 另外,当我尝试调试时,它不会总是在同一位置崩溃,所以我很难找到这个错误的根源......

有人知道发生了什么事吗?

I'm trying to load a hashmap by parsing a plist file in Android, using the plist parser from here: https://github.com/ZhouWeikuan/cocos2d.
This has been working fine in the past, but recently my program has been getting null-pointer exceptions when using this hashmap (after parsing).
When I try to debug, eclipse starts acting weird. The parser returns the hashmap and I can look over it's value in the variables view. When I step over the line that's assigning to 'worldMap', eclipse hangs. When trying to see worldMap's value in debug mode, I can see an empty line but no value - eventually eclipse crashes.

Map worlds = (Map)getWorlds().get("Worlds");
Map worldMap = (Map)worlds.get(String.valueOf(world));
Map levels = (Map)worldMap.get("Levels");

However, when running the program normally the null-pointer exception comes later on, past these lines.
Also, when I'm trying to debug, It won't always crash at the same location so I'm having a really hard time finding the source of this bug...

Does anyone know whats going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-12-14 22:10:18

无法确切地告诉您发生了什么,但 NullPointerException 肯定是一个很大的提示。

在上面示例中的每行代码后面添加一些空检查,我打赌您将能够很快找到错误:

Map whatEverThisIs = (Map)getWorlds();
if (whatEverThisIs == null) {
   /* Do something here */
}
Map worlds = (Map)whatEverThisIs.get("Worlds");
if (worlds == null) {
   /* Do something here */
}
Map worldMap = (Map)worlds.get(String.valueOf(world));
if (worldMap == null) {
   /* Do something here */
}
Map levels = (Map)worldMap.get("Levels");
if (levels == null) {
   /* Do something here */
}

Can't tell you for certain what is going on, but the NullPointerException is sure a big hint.

Add in some null checks after each line of code in your above example and I bet you will be able to track down the error pretty fast:

Map whatEverThisIs = (Map)getWorlds();
if (whatEverThisIs == null) {
   /* Do something here */
}
Map worlds = (Map)whatEverThisIs.get("Worlds");
if (worlds == null) {
   /* Do something here */
}
Map worldMap = (Map)worlds.get(String.valueOf(world));
if (worldMap == null) {
   /* Do something here */
}
Map levels = (Map)worldMap.get("Levels");
if (levels == null) {
   /* Do something here */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文