0

i have four pages on my jsp web project for student showing details from DB.. I have used tomcat web server.

index.html

<body>
    <h1> Student Search Page</h1>
    <form action="second.jsp">
        <h2> Enter id to search details</h2>
        <input type=text name=ID1 /> <br/>
        <input type=submit value=Search />
        </form>
</body>

second.jsp

<body>
   <%
   String id =(request.getParameter("ID1"));
   StudentDAO std=new StudentDAO();
      Student s=std.searchinfo("id");

   %>
   <h1> <%=s.showStudent()%></h1>
</body>

Student.java

 import java.io.*;

 public class Student implements Serializable {

String name; 
String id1,phone,clas;
Student(String  id,String n,String c,String ph)
{
     name=n;
     id1=id;
     phone=ph;
    clas=c;
}
public String showStudent()
{
   return  "Name: "+name+" Address: "+clas+" Phone: "+phone+" ID"+id1;
}

}

studentDAO.java

import java.sql.*;

import java.io.*; 

public class StudentDAO implements  Serializable {

Connection conn;
    PreparedStatement stmt;

public Student searchinfo(String id)
{
    Student studentinfo=null;

try{

String url="jdbc:ucanaccess://C:\\Users\\Asim Iqbal\\Documents\\STUDENT.accdb";
conn = DriverManager.getConnection(url);
String sql= "SELECT * FROM StudentDetails WHERE ID=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
ResultSet rs= null;
        try {
            rs = stmt.executeQuery();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            while (rs.next())
            {
               String i=rs.getString("ID");
               String nam=rs.getString("Name");                  
               String ph=rs.getString("Phone");
               String clas=rs.getString("Class");
                studentinfo=new Student(i,nam,ph,clas);
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return studentinfo;
 }


}

This is Stacktrace and root cause..

Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4
 76)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

 java.lang.NullPointerException

Student.StudentDAO.searchinfo(StudentDAO.java:35)

org.apache.jsp.second_jsp._jspService(second_jsp.java:104)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.23 logs.

Anptk
  • 1,125
  • 2
  • 17
  • 28
Faisal
  • 23
  • 1
  • 7
  • 1
    There could be any number of things wrong...You should get the `stacktrace` from the server and post that here so we can help you figure out what error is occurring... – brso05 Jun 26 '15 at 12:36
  • i have posted stacktrace and root cause.. bros05 – Faisal Jun 26 '15 at 12:59
  • looks like you have a `NullPointerException` at line 35 of `StudentDAO` method `searchInfo()`... – brso05 Jun 26 '15 at 13:26
  • In browser i get this type of error. 12: <%String id =(request.getParameter("ID1")); 13: 14: StudentDAO std=new StudentDAO(); 15: Student s=std.searchinfo("id"); 16: %> 17:

    <%= "s.showStudent()"%>

    18:
    – Faisal Jun 26 '15 at 13:37

3 Answers3

0

I am not sure what error your facing.Can you paste the error here so I can help you.But by seeing your JSP page I can say there is syntax error in the following line " <%=s.showStudent()%>". Give space between <%= and s.showStudent()%> EX: "<%= s.showStudent()%>"and try

Can you confirm that are you able to retrieve data from DB? Student object is null so your getting this error i think so

Thanks,

Thangaraj
  • 11
  • 2
0

org.apache.jasper.JasperException: An exception occurred processing JSP page /second.jsp at line 15

15: Student s=std.searchinfo("id");

bros05, this is the main error...

Faisal
  • 23
  • 1
  • 7
0

Your ID1 property not passed from index.xhtml to second.jsp.So you passing null value to StudentDAO.java which in turn ur getting null pointer exception.

try below fix in index.xhtml

<input type=text name="ID1" />
Thangaraj
  • 11
  • 2