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); } } } }
Whoops, I have an extra loop in there, but that aside, the problem persists.
Can you post the complete stack trace?
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.
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);
}
}
}
}
Whoops, I have an extra loop in there, but that aside, the problem persists.
Can you post the complete stack trace?
Have you tried casting the node to element and using setAttribute(name, value)
instead as a workaround? Could you provide a testcase?