gpt4 book ai didi

运行servlet时发生java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 07:56:21 25 4
gpt4 key购买 nike

大家好,我正在编写一个简单的注册表单来注册该 servlet。我已将所有条目包含在 web.xml 中。但我不知道为什么我收到“java.lang.NullPointerException”。一切对我来说看起来都很好,但我无法找出漏洞。请帮我解决这个问题。

index.jsp

<form action="InsertRecord" method="post">
<fieldset>
<legend>
<font face="Courier New" size="+1"
color="red">
Student Registration
</font>
</legend>
<table>
<tr>
<td>Student Name &nbsp;&nbsp; :</td>
<td><input type="text" name="name"></td>
</tr>

<tr>
<td>Enrollment Number &nbsp;&nbsp; :</td>
<td><input type="text" name="enrolmentNo"></td>
</tr>

<tr>
<td>Program Name &nbsp;&nbsp; :</td>
<td><input type="text" name="program"></td>
</tr>

<tr>
<td>Gender &nbsp;&nbsp; : </td>
<td><select Name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</td>
</tr>

<tr>
<td>Address &nbsp;&nbsp; :</td>
<td><textarea rows="3" cols="20"></textarea>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Registration"></td>
</table>
</fieldset>
</form>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>student</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>InsertRecord</servlet-name>
<servlet-class>com.varun.InsertRecord</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertRecord</servlet-name>
<url-pattern>/InsertRecord</url-pattern>
</servlet-mapping>
</web-app>

InsertRecord.java 这是 servlet

   package com.varun;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class InsertRecord extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

String name=request.getParameter("name");
String en=request.getParameter("enrolmentNo");
String program=request.getParameter("program");
String gender=request.getParameter("gender");
String address=request.getParameter("address");

int id=0;
int enrol=0;

if(name.equals("")||en.equals("")||program.equals("")
||gender.equals("")||address.equals(""))
{
out.println("Please insert the valid data");
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
rd.include(request, response);
}
else
{
out.println("good job");
RequestDispatcher rd=request.getRequestDispatcher("/sucess.jsp");
rd.include(request, response);
}


}

}

最佳答案

字符串地址为空。

String address=request.getParameter("address");

因为您的文本区域没有名称:

<td><textarea rows="3" cols="20"></textarea> 

修改为

<td><textarea rows="3" cols="20" name ="address"></textarea> 

并且异常应该消失。

关于运行servlet时发生java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com