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)
doGet()
method so.. – Vasan