0

I am trying to retrieve data from database but i am getting Null Pointer Exception in struts.I am using ecllipse mars.

Thanks in advance My files are:-

web.xml

enter code here

<?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_3_0.xsd" version="3.0">
<display-name>Report2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="myapp" />
<package name="default" extends="struts-default" namespace="/">
<action name="database" class="genius.database.ReportAction" 
method="execute">
<result name="Success">/ReportView.jsp</result>
</action>
</package>
</struts>

ReportAction.java

package genius.database;
import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import genius.database.Student;
public class ReportAction{
HttpServletRequest request=null;
public String execute() throws SQLException,ClassNotFoundException {
 Connection con;
 Class.forName("com.mysql.jdbc.Driver");
 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/registertable?
 zeroDateTimeBehavior=convertToNull","root","");
 Statement stmt=con.createStatement();
 ResultSet rs=stmt.executeQuery("select * from student");
 List<Student> li=null;
 li=new ArrayList<Student>();
 while(rs.next()){
        Student st=new Student();
        st.setRno(rs.getInt(1));
        st.setName(rs.getString(2));
        st.setEng(rs.getInt(3));
        st.setMaths(rs.getInt(4));
        li.add(st);
 }
 request.setAttribute("disp", li);
 return "Success";
}
}

Student.java

package genius.database;
public class Student {
private int rno;
private String name;
private int eng;
private int mat;
public int getRno() {
    return rno;
}

public void setRno(int rno) {
    this.rno = rno;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public int getEng() {
    return eng;
}

public void setEng(int eng) {
    this.eng = eng;
}

public int getMaths() {
    return mat;
}

public void setMaths(int mat) {
    this.mat = mat;
}
}

ReportView.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
  <%@taglib prefix="s"  uri="/struts-tags" %>
<%@page language="java" import="java.util.*"  %>
<%@page language="java" import="genius.database.Student" %>
<%@page language="java" import="genius.database.ReportAction" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Report</h2>
<s:actionerror key="error.Insert"/>
<s:form name="form"  method="post">
<table border="1">
    <thead>
        <tr>
            <td>Roll No</td>
            <td>Name</td>
            <td>Eng</td>
            <td>Maths</td>
        </tr>
    </thead>
    <tbody>
        <tr>
        <%  
            List<Student> li=(List<Student>)request.getAttribute("disp");
            out.println(li);
            if(li==null){
                Iterator<Student> it=li.iterator();
                while(it.hasNext()){
                        Student st=(Student)it.next();
                        int rno=st.getRno();
                        String name=st.getName();
                        int eng=st.getEng();
                        int mat=st.getMaths();
        %>
            <td><%out.println(rno);%></td>
            <td><%out.println(name);%></td>
            <td><%out.println(eng);%></td>
            <td><%out.println(mat);%></td>      
        <%      }
            }
        %>
        </tr>
    </tbody>
</table>
</s:form>


</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s"  uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Report</title>
</head>
<body>
<s:form action="database.action">
    <s:submit value="Submit"></s:submit>
</s:form>
</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Do you have the stacktrace from the Exception? I would post that as well. That usually has the best information as to where to start looking. – D.L. May 12 '17 at 06:38

2 Answers2

1

You probably mean:

if (li!=null)

From

        if(li==null){
            Iterator<Student> it=li.iterator();
            while(it.hasNext()){
                    Student st=(Student)it.next();
                    int rno=st.getRno();
                    String name=st.getName();
                    int eng=st.getEng();
                    int mat=st.getMaths();

Not sure if that's it, but that will definitely cause a NullPointerException.

D.L.
  • 314
  • 1
  • 7
0

Better to use JSTL rather than scriplets in you JSP

e.g.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
....

<c:forEach items="${disp}" var="student" >
   ${student.rno}
   ${student.name}
</c:forEach>

see https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64