Menu

someNode.appendChild(attr) broken?

Help
2010-07-28
2012-10-09
  • Nobody/Anonymous

    When I attempt to append a new attribute node onto a specific node, I get a
    null pointer exception from within jtidy -- Am I not initializing something or
    is there a bug? Here is my code:

    private void addTarget_blank(Document doc){
    NodeList list = doc.getElementsByTagName("a");
    for(int i=0; i<list.getLength(); i++){
    NamedNodeMap map = list.item(i).getAttributes();
    for (int j = 0; j < map.getLength(); j++) {
    if(map.getNamedItem("target") == null){
    Attr attr = doc.createAttribute("target");
    attr.setNodeValue("_blank");
    attr.setValue("_blank");
    list.item(i).appendChild(attr);
    }
    }
    }
    }

     
  • Nobody/Anonymous

    Whoops, I have an extra loop in there, but that aside, the problem persists.

     
  • aditsu

    aditsu - 2010-07-30

    Can you post the complete stack trace?

     
  • Martin

    Martin - 2010-09-15

    Have you tried casting the node to element and using setAttribute(name, value)
    instead as a workaround? Could you provide a testcase?

     

Log in to post a comment.