问题描述
我正在尝试用 Java 编写 StAX
线程main"中的异常;java.lang.NullPointerException 在org.example.shoesshop.parser.STAXParser.parse
这是 StAX Parser 的一个类:
公共类 STAXParser 扩展 DefaultHandler {私有静态列表<鞋子>parse
这是一个
还有一个用于鞋子的 Java 类
@
我还需要知道如何将接收到的数据写入新的
已更新: 对原始答案的评论:
它不起作用,它给出了同样的错误
这意味着问题是因为 shoes
变量是 null
,这在 debugger 中很容易看到.使用调试器会为我们节省很多时间,所以请开始使用调试器.
为了使 shoes
为 null
,看来代码遇到了一个不是鞋子
元素.
要修复代码,添加一个空检查,并在处理 Shoes
元素结束时设置 shoes = null
:
} else if (startElement.getName().getLocalPart().equals("title")) {if (shoes != null) {//<===== 添加这个shoes.setTitle(reader.getElementText());//<===== 解决这个问题(见原始答案)}
if (
原始答案
你的代码是:
} else if (startElement.getName().getLocalPart().equals("title")){
问题是代码没有检查如果事件跟在 START_ELEMENT
事件之后的类型.可能是这样的:
元素很可能是空的,即
<title/>
或<title><title/>
,在这种情况下下一个事件是END_ELEMENT
,asCharacters()
返回null
.元素有注释,例如
<title><!-- 没有标题--><title/>
,在这种情况下,下一个事件是COMMENT
.p>元素具有混合内容,例如
<title>foo<![CDATA[bar]]><title/>
,在这种情况下,下一个事件不是全文.
检索元素的文本内容是如此常见,以至于他们为此添加了一个辅助方法::
读取纯文本元素的内容.前提条件:当前事件是START_ELEMENT
.后置条件:当前事件是对应的END_ELEMENT
.
抛出: - 如果当前事件不是
START_ELEMENT
或者遇到非文本元素
这意味着你的代码应该是:
} else if (startElement.getName().getLocalPart().equals("title")) {shoes.setTitle(reader.getElementText());
I'm trying to write a StAX
Here's a class for StAX Parser:
public class STAXParser extends DefaultHandler {
private static List<Shoes> parse
Here's an
<?
Also here's a Java class for Shoes
@
Also I need to know how to write a received data into a new
UPDATED: Comment to original answer:
That means the problem is because the shoes
variable is null
, as would have easily been seen with a debugger. Using a debugger would have saved us all a lot of time, so please start using one.
In order for shoes
to be null
, it appears that the code encountered a <title>
element that is not a child of a Shoes
element.
To fix the code, add a null-check, and also set shoes = null
at the end of processing the Shoes
element:
} else if (startElement.getName().getLocalPart().equals("title")) {
if (shoes != null) { // <===== ADD THIS
shoes.setTitle(reader.getElementText()); // <===== Fix this (see original answer)
}
if (
ORIGINAL ANSWER
Your code is:
} else if (startElement.getName().getLocalPart().equals("title")){
The problem is that the code isn't checking what type if event follows the START_ELEMENT
event. It could be that:
Most likely, the element is empty, i.e.
<title/>
or<title><title/>
, in which case the next event is anEND_ELEMENT
, andasCharacters()
returnednull
.The element has a comment, e.g.
<title><!-- there is no title --><title/>
, in which case the next event is aCOMMENT
.The element has mixed content, e.g.
<title>foo<![CDATA[bar]]><title/>
, in which case the next event is not the full text.
Retrieving the text content of an element is such a common thing that they added a helper method for that: :
Which means that your code should be:
} else if (startElement.getName().getLocalPart().equals("title")) {
shoes.setTitle(reader.getElementText());
这篇关于为什么在运行 StAX Parser 时出现 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!