Java - XML NodeList.item()抛出NullPointerException

时间:2017-04-26 18:01:44

标签: java xml sax saxparser

当我使用SAXParser解析Android XML布局并通过NodeList读取元素项时,我得到了NPE

NodeList list = e.getChildNodes(); // e is Element
for(int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if(node == null) {
        continue;
    }
    if(node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        // process somehow it
    }
}

布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:gravity="center_horizontal" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <FrameLayout android:id="@id/dbx_button_container" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true">
        <include android:layout_gravity="bottom" android:id="@id/dbx_button_bar" android:layout_height="fill_parent" layout="@layout/bottom_bar_light" />
    </FrameLayout>
    <LinearLayout android:orientation="vertical" android:id="@id/dbx_main_container" android:background="@color/lightBlueBackground" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/dbx_button_container">
        <TextView android:textSize="@dimen/dbx_action_bar_title_text_size" android:textColor="#ffffffff" android:gravity="center_vertical" android:id="@id/dbx_install_title" android:background="@color/actionBarBackground" android:paddingLeft="10.0dip" android:layout_width="fill_parent" android:layout_height="@dimen/dbx_action_bar_default_height" android:drawableLeft="@drawable/tab_dropbox" android:drawablePadding="3.0dip" />
        <View android:id="@id/dbx_separator" android:background="#ff000000" android:layout_width="fill_parent" android:layout_height="0.5dip" />
        <View android:id="@id/dbx_top_space" android:layout_width="fill_parent" android:layout_height="0.5dip" android:layout_weight="7.0" />
        <ImageView android:layout_gravity="center" android:id="@id/dbx_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_new" android:layout_weight="7.0" />
        <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">
            <TextView android:textStyle="bold" android:gravity="center" android:layout_gravity="center_horizontal" android:id="@id/dbx_install_main" android:paddingLeft="30.0dip" android:paddingRight="30.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5.0dip" />
            <TextView android:textAppearance="?android:textAppearanceSmall" android:textSize="12.0sp" android:gravity="center" android:layout_gravity="center_horizontal" android:id="@id/dbx_install_sub" android:paddingLeft="30.0dip" android:paddingRight="30.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineSpacingMultiplier="1.25" />
        </LinearLayout>
        <View android:id="@id/dbx_bottom_space" android:layout_width="fill_parent" android:layout_height="0.5dip" android:layout_weight="10.0" />
    </LinearLayout>
</RelativeLayout>

我递归地读取所有内部元素并突然得到NPE:

at com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListItem(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ParentNode.item(Unknown Source)

为什么会这样?如何避免此异常并只读取元素数据?

UPD: 有时NPE会被引入getLength():

at com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListGetLength(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ParentNode.getLength(Unknown Source)

UPD 2: 整个身体

public List<DomElement> getChildNodes() {
    ArrayList<DomElement> elements = new ArrayList<DomElement>();
    NodeList list = e.getChildNodes();
    for(int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if(node == null) {
            continue;
        }
        if(node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            elements.add(new DomElement(doc, element));
        }
    }
    return elements;
}

0 个答案:

没有答案