使用struts的java中空指针异常的错误

时间:2017-05-12 06:28:51

标签: java nullpointerexception

我正在尝试从数据库中检索数据,但我在struts中得到Null指针异常。我正在使用ecllipse mars。

提前致谢 我的档案是: -

的web.xml

function Store_Location_onChange(objThis) {
    if (objThis.value == "0") {

        document.getElementById("StoreLocation").style.display = "";
    }
    else {
        document.getElementById("StoreLocation").value = "";
        document.getElementById("StoreLocation").style.display = "none";
    }
}

struts.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>

ReportAction.java

<?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>

Student.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";
}
}

ReportView.jsp

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;
}
}

的index.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>

2 个答案:

答案 0 :(得分:1)

你可能意味着:

if (li!=null)

        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();

不确定是不是,但这肯定会导致NullPointerException。

答案 1 :(得分:0)

最好在JSP中使用JSTL而不是scriplet

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>

请参阅https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm