Why I get NullPointerException when running StAX Parser?

后端 未结 1 1759
不思量自难忘°
不思量自难忘° 2021-01-29 14:38

I\'m trying to write a StAX XML Parser in Java, but always get NullPointerException error. Please help me to solve this issue. Full problem:

Exception in thr

1条回答
  •  攒了一身酷
    2021-01-29 15:21

    UPDATED: Comment to original answer:

    It doesn't work, it gives the same error

    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 </code> element that is not a child of a <code>Shoes</code> element.</p> <p>To fix the code, add a null-check, and also set <code>shoes = null</code> at the end of processing the <code>Shoes</code> element:</p> <pre class="lang-java prettyprint-override"><code>} else if (startElement.getName().getLocalPart().equals("title")) { if (shoes != null) { // <===== ADD THIS shoes.setTitle(reader.getElementText()); // <===== Fix this (see original answer) } </code></pre> <pre class="lang-java prettyprint-override"><code>if (xmlEvent.isEndElement()) { EndElement endElement = xmlEvent.asEndElement(); if (endElement.getName().getLocalPart().equals("Shoes")) { shoesList.add(shoes); shoes = null; // <===== ADD THIS } } </code></pre> <hr /> <p><em><strong>ORIGINAL ANSWER</strong></em></p> <p>Your code is:</p> <pre class="lang-java prettyprint-override"><code>} else if (startElement.getName().getLocalPart().equals("title")){ xmlEvent = reader.nextEvent(); shoes.setTitle(xmlEvent.asCharacters().getData()); </code></pre> <p>The problem is that the code isn't checking what type if event follows the <code>START_ELEMENT</code> event. It could be that:</p> <ul> <li><p>Most likely, the element is empty, i.e. <code><title/></code> or <code><title><title/></code>, in which case the next event is an <code>END_ELEMENT</code>, and <code>asCharacters()</code> returned <code>null</code>.</p> </li> <li><p>The element has a comment, e.g. <code><title><!-- there is no title --><title/></code>, in which case the next event is a <code>COMMENT</code>.</p> </li> <li><p>The element has mixed content, e.g. <code><title>foo<![CDATA[bar]]><title/></code>, in which case the next event is not the full text.</p> </li> </ul> <p>Retrieving the text content of an element is such a common thing that they added a helper method for that: getElementText():</p> <blockquote> <p>Reads the content of a text-only element. Precondition: the current event is <code>START_ELEMENT</code>. Postcondition: The current event is the corresponding <code>END_ELEMENT</code>.</p> <p><strong>Throws:</strong><br /> <code>XMLStreamException</code> - if the current event is not a <code>START_ELEMENT</code> or if a non text element is encountered</p> </blockquote> <p>Which means that your code should be:</p> <pre class="lang-java prettyprint-override"><code>} else if (startElement.getName().getLocalPart().equals("title")) { shoes.setTitle(reader.getElementText()); </code></pre> </p> <div class="appendcontent"> </div> </div> <div class="jieda-reply"> <span class="jieda-zan button_agree" type="zan" data-id='1909710'> <i class="iconfont icon-zan"></i> <em>0</em> </span> <span type="reply" class="showpinglun" data-id="1909710"> <i class="iconfont icon-svgmoban53"></i> 讨论(0) </span> <div class="jieda-admin"> </div> </div> <div class="comments-mod " style="display: none; float:none;padding-top:10px;" id="comment_1909710"> <div class="areabox clearfix"> <form class="layui-form" action=""> <div class="layui-form-item"> <label class="layui-form-label" style="padding-left:0px;width:60px;">发布评论:</label> <div class="layui-input-block" style="margin-left:90px;"> <input type="text" placeholder="不少于5个字" AUTOCOMPLETE="off" class="comment-input layui-input" name="content" /> <input type='hidden' value='0' name='replyauthor' /> </div> <div class="mar-t10"><span class="fr layui-btn layui-btn-sm addhuidapinglun" data-id="1909710">提交评论 </span></div> </div> </form> </div> <hr> <ul class="my-comments-list nav"> <li class="loading"> <img src='https://www.e-learn.cn/qa/static/css/default/loading.gif' align='absmiddle' />  加载中... </li> </ul> </div> </li> </ul> <div class="layui-form layui-form-pane"> <form id="huidaform" name="answerForm" method="post"> <div class="layui-form-item layui-form-text"> <a name="comment"></a> <div class="layui-input-block"> <script type="text/javascript" src="https://www.e-learn.cn/qa/static/js/neweditor/ueditor.config.js"></script> <script type="text/javascript" src="https://www.e-learn.cn/qa/static/js/neweditor/ueditor.all.js"></script> <script type="text/plain" id="editor" name="content" style="width:100%;height:200px;"></script> <script type="text/javascript"> var isueditor=1; var editor = UE.getEditor('editor',{ //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个 toolbars:[['source','fullscreen', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'link', 'unlink', 'anchor', '|', 'simpleupload', 'insertimage', 'scrawl', 'insertvideo', 'attachment', 'map', 'insertcode', '|', 'horizontal', '|', 'preview', 'searchreplace', 'drafts']], initialContent:'', //关闭字数统计 wordCount:false, zIndex:2, //关闭elementPath elementPathEnabled:false, //默认的编辑区域高度 initialFrameHeight:250 //更多其他参数,请参考ueditor.config.js中的配置项 //更多其他参数,请参考ueditor.config.js中的配置项 }); editor.ready(function() { editor.setDisabled(); }); $("#editor").find("*").css("max-width","362px"); </script> </div> </div> <div class="layui-form-item"> <label for="L_vercode" class="layui-form-label">验证码</label> <div class="layui-input-inline"> <input type="text" id="code" name="code" value="" required lay-verify="required" placeholder="图片验证码" autocomplete="off" class="layui-input"> </div> <div class="layui-form-mid"> <span style="color: #c00;"><img class="hand" src="https://www.e-learn.cn/qa/user/code.html" onclick="javascript:updatecode();" id="verifycode"><a class="changecode" href="javascript:updatecode();"> 看不清?</a></span> </div> </div> <div class="layui-form-item"> <input type="hidden" value="933962" id="ans_qid" name="qid"> <input type="hidden" id="tokenkey" name="tokenkey" value=''/> <input type="hidden" value="Why I get NullPointerException when running StAX Parser?" id="ans_title" name="title"> <div class="layui-btn layui-btn-disabled" id="ajaxsubmitasnwer" >提交回复</div> </div> </form> </div> </div> <input type="hidden" value="933962" id="adopt_qid" name="qid" /> <input type="hidden" id="adopt_answer" value="0" name="aid" /> </div> <div class="layui-col-md4"> <!-- 热门讨论问题 --> <dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">热议问题</dt> <!-- 本周热门讨论问题显示10条-->