解析 xml:attributes.getValue() 返回 null?
请参阅我的 xml 的这一部分:
<blocktype>
<properties name="normal_blue" type="1"/>
<resources image="brick_blue"/>
<properties powerup="0" shield="0" />
</blocktype>
我正在使用此代码来解析它:
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("level")) {
block = null;
inLevel = true; // parsing level
Log.d("MyContentHandler", "Parsing Level = " + Boolean.toString(inLevel));
} if (localName.equalsIgnoreCase("blocktype")) {
bundle = null;
inBlockType = true; // parsing blocktype
Log.d("MyContentHandler", "Parsing blocktype = " + Boolean.toString(inBlockType));
};
// handling level parsing
if (inLevel) {
int n = 0;
if (localName.equalsIgnoreCase("properties")) {
int c = Integer.parseInt(attributes.getValue("columns"));
int r = Integer.parseInt(attributes.getValue("rows"));
bundle = new XmlLevelBundle(r, c);
bundle.name = attributes.getValue("name");
}// bundle might be null?
if (localName.equalsIgnoreCase("block")) {
bundle.types[n] = Integer.parseInt(attributes.getValue("type"));
n++;
}
}
// handling BlockType parsing
if (inBlockType) {
if (localName.equalsIgnoreCase("properties")) {
block.name = attributes.getValue("name");
block.type = Integer.parseInt(attributes.getValue("type")); //nullpointer?
} else if (localName.equalsIgnoreCase("resources")) {
block.resourceName = attributes.getValue("image");
} else if (localName.equalsIgnoreCase("properties")) {
block.powerUp = Integer.parseInt(attributes.getValue("powerup"));
block.shield = Integer.parseInt(attributes.getValue("shield"));
}
}
}
现在,当我删除第二条语句时,一切似乎都正常。正如你所看到的,我的 xml 实际上具有 type
属性,但根据以下 logcat,它仍然返回 null:
12-09 18:53:30.741:警告/系统错误(851): java.lang.NumberFormatException:无法将“null”解析为整数 12-09 18:53:30.769: 警告/System.err(851): 在 java.lang.Integer.parseInt(Integer.java:406) 12-09 18:53:30.769: 警告/系统错误(851):位于 java.lang.Integer.parseInt(Integer.java:382) 12-09 18:53:30.779: 警告/系统错误(851):位于 cd.ark.utils.MyContentHandler.startElement(MyContentHandler.java:57)
...(抱歉格式错误)
所以基本上它说 Null 正在传递到 Integer.parseInt() 中。
那么有人可以帮我解决这个 NullPointerException 吗? 我已经被这个问题困扰了很长时间了。 谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于标签之一是“saxparser”,我假设这就是您正在使用的。在这种情况下,方法调用看起来是正确的,问题在于
attributes
未使用 XML 包含的值正确初始化。请添加代码以显示如何生成属性
。编辑:问题可能是
返回 null,因为您没有使用 限定名称;请参阅有关 SAX 库的文档,特别是 Attributes.getValue(String)。如果您使用
Attributes.getValue(String)
并且限定名称不可用,它将返回null
。关于 SO 限定名称的有用但简短的解释可以在 此处。
EDIT2:我不知道如何正确调整XML(我自己从未使用过XML),但是你不必使用命名空间,你也许可以通过使用
Attributes.getValue(int)
因为它不适用于属性名称,但适用于它所保存的属性列表。不过,您可能需要弄清楚属性的顺序;你可以这样解决:希望有帮助;也许你可以在 SO 上的 XML 命名空间中找到一些有用的东西;否则,如果您的程序需要使用属性名称来寻址,您可能必须了解 XML 命名空间。
Since one of the tags is "saxparser", I'm assuming that's what you're using. In that case, the method calls seem correct, leaving the problem to be that
attributes
isn't correctly initialized with the values that your XML contains. Please add code to show how you generateattributes
.EDIT: The problem is probably that
returns null since you aren't using qualified names in your XML; see the documentation on the SAX library, specifically the documentation of Attributes.getValue(String). If you use
Attributes.getValue(String)
and qualified names aren't available, it will returnnull
.A useful, but brief explanation of qualified names on SO can be found here.
EDIT2: I don't know how to properly adjust the XML (never used XML myself), but you don't have to use namespaces, you might be able to do what you want by using
Attributes.getValue(int)
since that doesn't work on the names of the attributes but on the list of attributes it holds. You probably need to figure out the order of the attributes though; you could figure that out this way:Hope that helps; maybe you can find something helpful on namespaces in XML on SO; otherwise if your program requires the use of addressing attributes by their name, you probably will have to learn about XML namespaces.