解析 xml:attributes.getValue() 返回 null?

发布于 2024-12-20 08:42:12 字数 2670 浏览 3 评论 0 原文

请参阅我的 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 吗? 我已经被这个问题困扰了很长时间了。 谢谢!

See this part of my xml:

<blocktype>
    <properties name="normal_blue" type="1"/>
    <resources image="brick_blue"/>
    <properties powerup="0" shield="0" />
</blocktype>

I'm using this code to parse it:

@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"));
        }
    }
}

Now everything seems to work when I take away the second statement. As you can see my xml actually has the type attribute, still it returns null, according to the following logcat:

12-09 18:53:30.741: WARN/System.err(851):
java.lang.NumberFormatException: unable to parse 'null' as integer
12-09 18:53:30.769: WARN/System.err(851): at
java.lang.Integer.parseInt(Integer.java:406) 12-09 18:53:30.769:
WARN/System.err(851): at
java.lang.Integer.parseInt(Integer.java:382) 12-09 18:53:30.779:
WARN/System.err(851): at
cd.ark.utils.MyContentHandler.startElement(MyContentHandler.java:57)

...(sorry for formatting)

So basically it says Null is being passing into Integer.parseInt().

So could anyone help me with this NullPointerException?
I've been stuck on this for a long time.
Thanks!

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

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

发布评论

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

评论(1

日久见人心 2024-12-27 08:42:12

由于标签之一是“saxparser”,我假设这就是您正在使用的。在这种情况下,方法调用看起来是正确的,问题在于 attributes 未使用 XML 包含的值正确初始化。请添加代码以显示如何生成属性

编辑:问题可能是

attributes.getValue("rows")

返回 null,因为您没有使用 限定名称;请参阅有关 SAX 库的文档,特别是 Attributes.getValue(String)。如果您使用 Attributes.getValue(String) 并且限定名称不可用,它将返回 null

关于 SO 限定名称的有用但简短的解释可以在 此处

EDIT2:我不知道如何正确调整XML(我自己从未使用过XML),但是你不必使用命名空间,你也许可以通过使用Attributes.getValue(int) 因为它不适用于属性名称,但适用于它所保存的属性列表。不过,您可能需要弄清楚属性的顺序;你可以这样解决:

for(int i = 0; i < attributes.getLength(); i++) {
    System.out.println(attributes.getValue(i));
}

希望有帮助;也许你可以在 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 generate attributes.

EDIT: The problem is probably that

attributes.getValue("rows")

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 return null.

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:

for(int i = 0; i < attributes.getLength(); i++) {
    System.out.println(attributes.getValue(i));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文