【问题标题】:Null Pointer Exception on retrieving multiple select values in JSP在 JSP 中检索多个选择值时出现空指针异常
【发布时间】:2020-10-23 20:59:12
【问题描述】:

使用 JSP,在我的 HTML 中,我有一个提交到另一个页面的表单。该表单有一个包含多个元素的选择列表。在提交表单之前,在 java 脚本中,我确保选择了列表中的所有元素。然后我将表单提交到下一页。在下一页中,我将列表中的选定值分配给 java 数组。当我尝试显示数组的大小或该数组中的任何值时,我得到空指针异常。

这是我的表格和列表:

<form name="inputGenesForm" id="test" method="POST" ACTION="result.jsp" enctype="multipart/form-data">          
    <div style=" position:absolute;top:6%;left:39%;">
    ...
    ...
    <select id="inputSet2" multiple="multiple" style="position:absolute; top: 93px; left: 270px; width: 200px; height: 200px;">

在result.jsp中,

<body background="image/geneBG.jpg">
<%       
    String [] selectedGenes= request.getParameterValues("inputSet2");
%>

<script>    
    alert(<%=selectedGenes.length%>);
</script>

有人可以帮忙吗?谢谢。

向 result.jsp 提交表单时出错:

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.

【问题讨论】:

    标签: java html jsp


    【解决方案1】:

    request.getParameterValues() 不能与 "enctype="multipart/form-data" 结合使用

    【讨论】:

      【解决方案2】:

      尝试在选择中添加名称属性

      <select name="inputSet2" ...>
      

      " 只有具有 name 属性的表单元素在提交表单时才会传递其值。" http://www.w3schools.com/tags/att_input_name.asp

      【讨论】:

      • 我添加了名称,但它仍然为空。
      【解决方案3】:

      在 results.jsp 文件中提醒这一点:

      alert(<%=request.getParameterValues("inputSet2")%>);
      

      并删除这一行:

      String [] selectedGenes= request.getParameterValues("inputSet2");
      

      确定 inputSet2 字段是否为空。

      此外,为了能够查看发送到结果页面的内容,请更改您的表单发布方法以获取:

      <form name="inputGenesForm" id="test" method="get" action="result.jsp">
      

      如果你不能缩小到下面的问题,我做了一个小测试,然后开始工作。

      form.jsp

      <html>
      <head>
      <title>Testing MultiSelect</title>
      <script>
      var validate= function() {
         if(document.forms.form1.select2.selectedIndex == -1) {
              alert("Please select one or more options");
              return false;
         }
         return true;
      };
      </script>
      </head>
      <body>
      <form id="form1" name="form1" method="get" action="result.jsp">
            <select name="select2" size="3" multiple="multiple" tabindex="1">
              <option value="1">one</option>
              <option value="2">two</option>
              <option value="3">three</option>
              <option value="4">four</option>
              <option value="5">five</option>
              <option value="6">six</option>
              <option value="7">seven</option>
              <option value="8">eight</option>
              <option value="9">nine</option>
              <option value="10">ten</option>
            </select>
            <br>
            <input type="submit" name="Submit" value="Submit" tabindex="2" onclick="return validate()" />
      </form>
      </body>
      </html>
      

      result.jsp

      <html>
      <head>
      <title>Results Page</title>
      </head>
      <body>
      <h1>These Are Your Results</h1>
      <p>
      <% String [] selectedGenes = request.getParameterValues("select2");
         for (String selectedGene : selectedGenes)
              out.println(selectedGene + "<br>");
      %>
      </body>
      </html>
      

      我选择了前三个选项,get url是

      result.jsp?select2=1&select2=2&select2=3&Submit=Submit
      

      【讨论】:

      • 啊哈。该值为空。为什么?我犯了什么错误?
      • 从我看来你的代码应该可以工作,所以让我们继续向后调试。现在将该警报切换到 request.getParameter("inputSet2");
      • 听起来好像与表单的发布方式有关?在您的表单页面中尝试这个,这样您就可以准确地看到发送到 results.jsp 的内容:
      • Lucas Merencia 是对的,您在选择中缺少名称属性。我完全错过了,但上面会表明 inputSet2 没有过来。
      • 我添加了名称,但它仍然为空。
      【解决方案4】:

      你不应该先检查这个是否有空值吗?

      selectedGenes.length
      

      getParameterValues() 的文档说:

      返回包含所有值的 String 对象数组 给定的请求参数有,如果参数不存在,则 null.

      (我的重点)

      【讨论】:

      • 我检查了 if(selectedGenes==null) alert("null!");但我得到了同样的错误!
      【解决方案5】:

      我遇到了同样的问题,我解决了:

      String [] selectedGenes = request.getParameterValues("select2[]");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多