I started learning Java recently and I am facing lot of simple but irritating issues. I spent a lot of time trying to figure out the problem in vain.
I am trying to run a simple registration (with 3 pages) and submit the values into a DB. I am getting `NullPointerException` and not sure how to proceed with debugging any help will be greatly appreciated.
`form1.html`
Form details:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="./registration" method="get">
Name:<input type="text" name="name"><br/>
Father Name: <input type="text" name="fname"><br/>
Mother Name: <input type="text" name="mname"><br/>
<input type="hidden" name="formd" value="1"/>
<input type="submit" value="Next>>>">
</form>
</body>
</html>
`form2.html`
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="./registration">
Contact:<input type="text" name= "contact"><br/>
Email: <input type= "text" name= "email"><br/>
Address: <input type ="text" name= "address"><br/>
<input type="hidden" name= "formd" value="2"/>
<input type= "submit" value="Next>>>">
</form>
</body>
</html>
`form3.html`
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="./registration">
Qualification:<input type="text" name= "qualification"><br/>
Percentage: <input type ="text" name= "percentage"><br/>
<input type="hidden" name= "formd" value="3"/>
<input type= "submit" value="Submit!">
</form>
</body>
</html>
**My Servlet**
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AadharRegistration extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession httpsession = request.getSession();
String fno = request.getParameter("formd");
if (fno.equals("1")) {
String name = request.getParameter("name");
String fname = request.getParameter("fname");
String mname = request.getParameter("mname");
httpsession.setAttribute("name", name);
httpsession.setAttribute("fname", fname);
httpsession.setAttribute("mname", mname);
response.sendRedirect("./form2.html");
}
if (fno.equals("2")) {
String contact = request.getParameter("contact");
String email = request.getParameter("email");
String address = request.getParameter("address");
httpsession.setAttribute("contact", contact);
httpsession.setAttribute("email", email);
httpsession.setAttribute("address", address);
response.sendRedirect("./form3.html");
}
if (fno.equals("3")) {
String qualification = request.getParameter("qualification");
String percentage = request.getParameter("percentage");
String name = (String) httpsession.getAttribute("name");
String fname = (String) httpsession.getAttribute("fname");
String mname = (String) httpsession.getAttribute("mname");
String contact = (String) httpsession.getAttribute("contact");
String email = (String) httpsession.getAttribute("email");
String address = (String) httpsession.getAttribute("address");
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:p1aor01", "system",
"sysdba");
PreparedStatement ps = conn.prepareStatement("insert into aadhar values (?,?,?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, fname);
ps.setString(3, mname);
ps.setString(4, contact);
ps.setString(5, email);
ps.setString(6, address);
ps.setString(7, qualification);
ps.setString(8, percentage);
int res = ps.executeUpdate();
if (res != 0) {
out.println("<font color='green'><h1>Registered Successfully!</h1>");
}
} catch (SQLException e) {
out.println("<font color='red'><h1>Registration exception Failed!</h1>");
e.printStackTrace();
} catch (ClassNotFoundException xe) {
xe.printStackTrace();
}
}
}
}
I forgot to add Web.xml. please see below. If i use Dynamic web module version 3.1,i read that we do not need web.xml. Can i still use it? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> TestAadharRegistration AadharRegistration AadharRegistration controller.AadharRegistration AadharRegistration /registration
**Error:**
java.lang.NullPointerException
controller.AadharRegistration.doGet(AadharRegistration.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
From Console in eclipse:
SEVERE: Servlet.service() for servlet [AadharRegistration] in context with path [/TestAadharRegistration] threw exception
java.lang.NullPointerException
at controller.AadharRegistration.doGet(AadharRegistration.java:26)
答案 0 :(得分:0)
由于某种原因,这一行
if (fno.equals("1")) {
抛出NullPointerException。
这表示您的表单未发送该值。即这是空的:
String fno = request.getParameter("formd");
在对数据执行各种操作之前,检查它是否为空是很重要的。否则,如果为null,则会出现NullPointerException。您可以像这样检查null:
if(fno != null && fno.equals("1")){
在这种情况下,您需要找出fno
为空的原因,因为如果您通过表单发送了值,则不应该这样。这是浏览器开发人员工具可用的地方。
如果您使用的是Chrome,请右键单击您的页面,然后单击“检查”..然后单击网络面板。现在确保您已选中顶部的“保留日志”复选框,然后再次提交表单。您将看到一系列事情发生,点击带有您的URL的那个,然后查看标题。向下滚动,会出现一个名为“查询字符串参数”的部分,在这里您可以看到您的表单发送到您的servlet的内容。如果没有“formd”,那么这就是你得到NullPointerException的原因。
答案 1 :(得分:0)