Java ArrayList class cast exception when trying to set an member variable for an instance of a class -
hi have been stuck on error past 3 hours , slap myself when figures out wrong. trying set arraylist instance of teacheraccount class. keep getting below error when running simplest test think can below.
here teacheraccount class
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; } }
and here small test running produces error
teacheraccount currentteach = new teacheraccount(); list<string> tempstu = new arraylist<string>(); tempstu.add("hi"); tempstu.add("hi again"); currentteach.setstudents(tempstu);
i have narrowed down the problem being setstudents. tempstu , students both of type list appreciated. thanks
edit here jsp file requested:
<%@ 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 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 students!"> </form> </body> </html> [error ] srve0777e: exception thrown application class 'com._jsp._homepage._jspservice:98' java.lang.classcastexception: java.util.arraylist cannot cast java.lang.string @ com._jsp._homepage._jspservice(_homepage.java:98) @ com.ws.jsp.runtime.httpjspbase.service(httpjspbase.java:101) @ javax.servlet.http.httpservlet.service(httpservlet.java:668) @ [internal classes] @ com.ws.jsp.webcontainerext.abstractjspextensionservletwrapper.handlerequest(abstractjspextensionservletwrapper.java:215) @ com.ws.webcontainer.filter.webappfiltermanager.invokefilters(webappfiltermanager.java:1033) @ [internal classes] @ com.studentorganizer.login.doget(login.java:93) @ javax.servlet.http.httpservlet.service(httpservlet.java:575) @ [internal classes] [err] java.lang.classcastexception: java.util.arraylist cannot cast java.lang.string [err] @ com._jsp._homepage._jspservice(_homepage.java:98) [err] @ com.ws.jsp.runtime.httpjspbase.service(httpjspbase.java:101) [err] @ javax.servlet.http.httpservlet.service(httpservlet.java:668) [err] @ [internal classes]
i'm not strong in jsp clear me , try show students
in jsp file single string. students
defined list. please, run in loop in jsp file on students.
something like:
<% arraylist<string> students = teacheraccount .getstudents(); iterator<string> iterator = students .iterator(); while (iterator.hasnext()) { string student = (string)iterator.next(); string pid = string.getid(); ... %>
suppose problem here:
<div>test jsp vars <%=currentusername%> pass: <%=currentpassword%> students: <%=students%></div> ^^^^
use foreach
instead show it
Comments
Post a Comment