【发布时间】:2015-12-05 22:02:38
【问题描述】:
我正在处理如下代码的空指针异常:
<%
SessionData session = getSessionData(request);
Webpage webPage = null;
if (session!= null) {
webPage = session.getWebPage();
}
%>
<script type="text/javascript">
//NullPointer happens here, webPage is null when the session is lost
<tags:ComboBox
comboBox="<%=webPage.getComboBox()%>" />
</script>
当我可以将 if (session!=null 的结尾移到 javascript 之后时,我感到很惊讶,当会话为空时,它似乎忽略了该代码。
<%
SessionData session = getSessionData(request);
Webpage webPage = null;
if (session!= null) {
webPage = session.getWebPage();
//} move this to below
%>
<script type="text/javascript">
//NullPointer happens here, webPage is null when the session is lost
<tags:ComboBox
comboBox="<%=webPage.getComboBox()%>" />
</script>
<% } %> //moved to here
括号内的ComboBox 标签的scriptlet 是否不再运行?我认为它仍然会尝试将组合框从网页上移开,但最终仍会得到一个空指针。我是否认为 scriptlet 在代码实际运行之前都获得了它们的值是不正确的?
(我只是想提一下,如果没有会话,有一个包含的脚本会重定向页面。我得到一个带有第一部分代码的 NullPointer,并正确重定向到第二部分)
【问题讨论】: