尝试为类的实例设置成员变量时,Java ArrayList类强制转换异常

时间:2013-08-15 18:11:47

标签: java arraylist classcastexception

嗨,我在过去的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和学生都是类型列表任何帮助表示赞赏。感谢

编辑这是所要求的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]

1 个答案:

答案 0 :(得分:6)

我在JSP方面不强,但对我来说很明显,你试图在JSP文件中显示students,就像单个String一样。但是students被定义为List。请在学生的JSP文件中循环运行。

类似的东西:

<%
ArrayList<String> students = teacherAccount .getStudents();
    Iterator<String> iterator = students .iterator();

    while (iterator.hasNext())
    {
      String student = (String)iterator.next();
      String pId = String.getId();
      ...
%>

假设你的问题在这里:

<div>Test for jsp vars <%=currentUsername%> 
PASS: <%=currentPassword%> STUDENTS: <%=students%></div>
                                           ^^^^     

使用forEach代替显示