2012-01-17 82 views
6

的Android的SDK的org.w3c.dom.Node.insertBefore的描述说以下内容:org.w3c.dom.Node.insertBefore:NullPointerException,Bug?

public abstract Node insertBefore (Node newChild, Node refChild)
Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.

但是,如果我这样做,我得到的是发生在执行的insertBefore NullPointerException异常:

if(doc != null && doc.getFirstChild() != null && tmpNode != null) 
    doc.getFirstChild().insertBefore(tmpNode, null); 

WARN/System.err(11029): at org.apache.harmony.xml.dom.InnerNodeImpl.insertBefore(InnerNodeImpl.java:86)

我想这与Android 2.2和Android 2.3.3!
对我来说,它似乎是一个错误。任何人都可以确认/重现吗?


//编辑(18.01.2012 13:05):
我创建了一个新的Java项目,因为我想看看,如果这个工程在Java应用程序:

public static void main(String[] args) { 
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder; 

     try { 
      docBuilder = dbfac.newDocumentBuilder(); 
      Document d = docBuilder.newDocument(); 

      if(d != null){ 
       d.appendChild(d.createElement("root")); 
       if(d.getFirstChild() != null){ 
        d.getFirstChild().insertBefore(d.createElement("foo"), null); 
        System.out.println(d.getFirstChild().getFirstChild().getNodeName()); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

此代码完美的作品。

我还创建了一个新的Android项目再次测试此:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder; 

    try { 
     docBuilder = dbfac.newDocumentBuilder(); 
     Document d = docBuilder.newDocument(); 

     if(d != null){ 
      d.appendChild(d.createElement("root")); 
      if(d.getFirstChild() != null){ 
       d.getFirstChild().insertBefore(d.createElement("foo"), null); 
       System.out.println(d.getFirstChild().getFirstChild().getNodeName()); 
      } 
     } 
    } catch (Exception e2) { 
     e2.printStackTrace(); 
    } 
} 

当应用达到的insertBefore,上面显示的异常被抛出。

所以相同的代码在普通的Java中工作,但不在Android中。对我来说,它似乎仍然是org.w3c.dom的apache协调实现中的一个错误。任何其他想法?

+1

我敢打赌这是你的代码,而不是图书馆。了解到你是这个问题是一个漫长而艰难的教训。 – duffymo 2012-01-17 10:26:14

+1

当然,这可能是我的错。那为什么我先在这里提出这个问题(在我提交错误之前)并寻求帮助或某人重现此问题。但那不是我在android中使用org.w3c.dom的apache-harmony实现时遇到的第一个问题 - 在实现中还存在其他错误或缺陷,这些问题已经得到开发人员的确认,并使我的生活更加艰难。所以也许有一些偏见^^ – Marc 2012-01-17 10:34:23

回答

0

在if语句中添加doc!=null。看来你的doc对象是null。

+0

我加了doc!= null,但是我得到了同样的异常。我很确定它是一个错误,因为如果给出一个有效的节点作为第二个参数,整个代码就可以工作。我会在我的原始文章中添加这个doc!= null检查,以便其他读者清楚。 – Marc 2012-01-17 10:23:30

3

可以从org.apache.harmony.xml.dom被复制,即使4.1.2 执行情况的insertBefore是

public Node insertBefore(Node newChild, Node refChild) throws DOMException { 
    LeafNodeImpl refChildImpl = (LeafNodeImpl) refChild; 

    if (refChildImpl.document != document) { 
     throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); 
    } 

    if (refChildImpl.parent != this) { 
     throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); 
    } 

    return insertChildAt(newChild, refChildImpl.index); 
} 

正如你可以看到没有人检查refChildImpl是空,因此refChildImpl.document!=document抛出NullPointerException 此实现不作任何意义,如果下面是正确

的Android的SDK的org.w3c.dom.Node.insertBefore描述说以下内容:

public abstract Node insertBefore(Node newChild,Node refChild)

在现有子节点refChild之前插入节点newChild。如果 refChild为空,则在子列表的末尾插入newChild。