我目前有代码围绕元素包装表:
public static Element wrapElementInTable(Element e)
{
if (e == null)
return null;
return e.wrap(createTableTemplate().outerHtml());
}
public static Element createTableTemplate()
{
return createElement("table", "").appendChild(
createElement("tr").appendChild(
createElement("td"))
);
}
Run Code Online (Sandbox Code Playgroud)
现在我在main方法中创建一个Element:
public static void main(String[] args) throws IOException
{
Element e = new Element(Tag.valueOf("span"),"");
String text = HtmlGenerator.wrapElementInTable(e).outerHtml();
System.out.println(text);
}
Run Code Online (Sandbox Code Playgroud)
问题是我在wrap方法中收到NullPointerException,显然没有理由.
Exception in thread "main" java.lang.NullPointerException
at org.jsoup.nodes.Node.wrap(Node.java:345)
at org.jsoup.nodes.Element.wrap(Element.java:444)
at usingjsoup.HtmlGenerator.wrapElementInTable(HtmlGenerator.java:56)
at usingjsoup.UsingJsoup.main(UsingJsoup.java:19)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么抛出NullPointerException?(如果我在调用wrap之前打印出元素,则输出是我创建的标记)
jsoup ×1