2

こんにちは、私は過去 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 と students はどちらも 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]
4

1 に答える 1