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

使用struts的java中出现空指针异常错误,java,nullpointerexception,Java,Nullpointerexception,我试图从数据库中检索数据,但在struts中出现空指针异常。我正在使用ecllipse mars 提前谢谢 我的档案是:- 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:sche

我试图从数据库中检索数据,但在struts中出现空指针异常。我正在使用ecllipse mars

提前谢谢 我的档案是:-

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

报告
你的意思可能是:

if (li!=null)

if(li==null){
迭代器it=li.Iterator();
while(it.hasNext()){
Student st=(Student)it.next();
int rno=st.getRno();
String name=st.getName();
int eng=圣赫滕();
int mat=st.GetMath();

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

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

e、 g


....
${student.rno}
${student.name}

请参见

您是否有异常的stacktrace?我也会发布它。这通常是关于从何处开始查找的最佳信息。
<%@ 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>
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();
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
....

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