gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:11:46 26 4
gpt4 key购买 nike

我正在尝试从数据库检索数据,但我在 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>

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>

最佳答案

您可能的意思是:

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。

关于java - java使用struts出现空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com