嗨,在过去的 3 个小时里,我一直被这个错误困扰,当有人找出问题所在时,我可能会打自己一巴掌。我真的只是想将一个 ArrayList 设置为我的 TeacherAccount 类的一个实例。在运行我认为可以的最简单测试时,我不断收到以下错误,这也在下面。
这是 TeacherAccount 类
package com.studentOrganizer;
import java.util.List;
public class TeacherAccount {
private String username;
private String password;
private List<String> students;
public TeacherAccount(){
username=null;
password=null;
students = null;
}
public TeacherAccount(String username, String password,List<String> students){
this.username = username;
this.password = password;
this.students = students;
}
public void setUsername(String username){
this.username = username;
}
public void setPassword(String password){
this.password = password;
}
public void setStudents(List<String> students){
this.students = students;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
public List<String> getStudents() {
return students;
}
}
这是我正在运行的产生错误的小测试
TeacherAccount currentTeach = new TeacherAccount();
List<String> tempStu = new ArrayList<String>();
tempStu.add("Hi");
tempStu.add("Hi again");
currentTeach.setStudents(tempStu);
我已经缩小了 setStudents 的问题范围。tempStu 和学生都是 List 类型的任何帮助表示赞赏。谢谢
编辑这里是请求的 JSP 文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% String currentUsername = (String)request.getAttribute("currentUsername"); %>
<% String currentPassword = (String)request.getAttribute("currentPassword"); %>
<% String students = (String)request.getAttribute("students"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/style.css">
<script src="js/StudentOrganizer.js" type="text/javascript"></script>
<script src="js/jquery-1.10.2.min.js"></script>
<title>Student Organization Home Page</title>
</head>
<body>
<form action="">
<div class='header'>Student Organization Home Page - Welcome <%=currentUsername%>!</div>
</form>
<br>
<div>Test for jsp vars <%=currentUsername%> PASS: <%=currentPassword%> STUDENTS: <%=students%></div>
<FORM METHOD="LINK" ACTION="jsp/students.jsp">
<select>
<option selected>Class 1</option>
<option selected>Class 2</option>
<option selected>Class 3</option>
<option selected>Class 4</option>
</select>
<INPUT TYPE="submit" VALUE="Go To Students!">
</FORM>
</body>
</html>
[ERROR ] SRVE0777E: Exception thrown by application class 'com._jsp._homepage._jspService:98'
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
at com._jsp._homepage._jspService(_homepage.java:98)
at com.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:101)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at [internal classes]
at com.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:215)
at com.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1033)
at [internal classes]
at com.studentOrganizer.Login.doGet(Login.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at [internal classes]
[err] java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
[err] at com._jsp._homepage._jspService(_homepage.java:98)
[err] at com.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:101)
[err] at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[err] at [internal classes]