Java XML error javax xml. transform. TransformerException: java. lang.NullPointerException

Java XML error javax xml. transform. TransformerException: java. Lang.nullpointerexception solution:

Use java to operate XML. In the process of operating XML, execute to the last step. NullPointerException error occurs when using transformer to convert XML. The code of the problem is as follows:

The following errors occur when running the transform function (relatively long, because I use swing to design the graphical interface, there will be some errors in the event call of the graphical interface):

At first, I was also surprised that the transform function only converts the XML tree in memory into a file. Why is there a nullpointer error. After checking on the Internet, no one found the answer. Finally( http://blog.awe.cz/post/english/beware-of-null-text-nodes/ )Find a very useful answer in this blog

In fact, if you look at the wrong information carefully, you will find that one information is more important:

It can be seen from here that it is due to com sun. org. apache. xml. internal. serializer. ToUnkNownStream. There is an error in the characters function in the java file. The number of lines is 317. Go to the JDK installation directory, find SRC, and find tounknownstream in the corresponding folder serializer Java, find the following function

Obviously, notice int length = chars length(); If the parameter chars is null, a nullpoiterexception error will appear when calling the length function. One more level, com sun. org. apache. xalan. internal. xsltc. Dom2to under Trax Parse function of java file (line 240):

Here, you can know why the null parameter appears in the characters function. The reason is that this node is a text node text_ Node, but there is no content in the calling node (null). When calling getnodevalue, null is returned

Rising to the transform function, the node content appears to be null. In the domsource class, because it is an XML tree established in memory, it must be that the content of one or more elements or nodes in the XML tree is null.

OK, find the cause of the problem. The solution is very simple. If an element or node in the domsource is null, it must be in the operation of XML, or in the operation of modifying content, adding elements, etc., so that the content becomes null. So what we need to do now is to find the code to modify or add the content of the element and deal with the possible null. If NULL is detected, we will not modify the XML or add this element. The easiest way is to use an IF statement. That is, before setnodevalue (string STR) or settextcontent (string STR), check whether the parameter STR is empty (if (STR = = null)). If it is empty, this function will not be called.

In my program, I do add empty content to the element. The code is as follows: deviceandidmap is a HashMap. When it calls get, it will return null if there is no primary key

Therefore, I can solve the problem by adding an IF statement. The code (this code is in the for loop, so use continue to skip this modification) is as follows:

Problem solved!

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>