[英]Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.util.ArrayList
I'm doing a library management project and when I register a member to the library is registers properly "the write method works fine." 我正在做一个库管理项目,当我向库中注册一个成员时,正确地注册“写入方法工作正常”。 the Error message is "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.ArrayList cannot be cast to comsatslibrary.Person strong text " 错误消息是“线程中的异常”AWT-EventQueue-0“java.lang.ClassCastException:java.util.ArrayList无法强制转换为comsatslibrary.Person 强文本 ”
private class listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String user = text.getText();
String pass = text1.getText();
boolean flag = true;
ArrayList<Person> personlist = HelperClass3.readAllData("person.txt");
if (e.getSource() == b) {
for (int i = 0; i < personlist.size(); i++) {
if ((user.equals((personlist.get(i).getFirstname()))) && (pass.equals(personlist.get(i).getPassword()))) {
flag = false;
}
}
if (flag == false) {
librarian l = new librarian();
}
} else{
JOptionPane.showMessageDialog(null, "Incorrect UserName or Password");
}
}
}
} }
if ((user.equals((personlist.get(i).getFirstname()))) && (pass.equals(personlist.get(i).getPassword())))
this is the line of error 这是错误的一行
I have created a generic helper Class 我创建了一个通用助手类
Read method 读方法
public class HelperClass3 {
public static <T> ArrayList<T> readAllData(String path){
ArrayList<T> List = new ArrayList<T>(0);
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(new FileInputStream(path));
boolean EOF = false;
while(!EOF) {
try {
T myObj = (T)inputStream.readObject();
List.add(myObj);
}catch (ClassNotFoundException e) {
System.out.println("Class not found");
}catch(EOFException e) {
EOF = true;
}
}
} catch(FileNotFoundException e) {
System.out.println("Cannot find file");
} catch(IOException e) {
System.out.println("IO Exception while opening stream");
} finally {
try {
if(inputStream!=null) {
inputStream.close();
}
}catch(IOException e) {
System.out.println("IO Exception while closing file");
}
}
return List;
}
write method 写方法
public static<T> void addArrayListToFile(T s , String path) {
ArrayList<T> List = readAllData(path);
List.add(s);
ObjectOutputStream outputStream =null;
try {
outputStream = new ObjectOutputStream(new FileOutputStream(path));
for(int i = 0 ; i < List.size() ; i++) {
outputStream.writeObject(List.get(i));
}
} catch(IOException e) {
System.out.println("IO Exception while opening file");
}
finally {
try {
if(outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
System.out.println("IO Exception while closing file");
}
}
}
}
T myObj = (T)inputStream.readObject(); T myObj =(T)inputStream.readObject(); this is where the problem is. 这就是问题所在。
Your action listener attempts to read Person objects from the file "person.txt" using an ObjectInputStream. 您的动作侦听器尝试使用ObjectInputStream从文件“person.txt”中读取Person对象。 As an aside, I would recommend a different file extension. 顺便说一句,我会推荐一个不同的文件扩展名。 ".txt" implies a text file, but this file contains Objects. “.txt”表示文本文件,但此文件包含对象。 It's a sort of binary file rather than pure text. 它是一种二进制文件而不是纯文本。
You didn't show us the contents of the file, or how you call the write method to populate the file; 您没有向我们展示文件的内容,或者您如何调用write方法来填充文件; but judging by the name of the method, addArrayListToFile
, I presume you pass an ArrayList
to the method. 但是通过方法的名称addArrayListToFile
来判断,我假设您将ArrayList
传递给该方法。 You then read the contents of the file, add the whole ArrayList as a single element, and write it back. 然后,您读取文件的内容,将整个ArrayList作为单个元素添加,然后将其写回。 The problem is here: 问题出在这里:
List.add(s);
The argument s
is an ArrayList, so you add this object as a single element to List
. 参数s
是一个ArrayList,因此您将此对象作为单个元素添加到List
。 So your file doesn't contain Person objects; 所以你的文件不包含Person对象; it in fact contains ArrayList objects. 它实际上包含ArrayList对象。 When you execute this statement: 执行此语句时:
if ((user.equals((personlist.get(i).getFirstname()))) && (pass.equals(personlist.get(i).getPassword()))) {
since personlist
is a generic (parameterized) type, the compiler automatically tries to cast personlist.get(i)
to a Person
object, but since it's in fact an ArrayList, you get the ClassCastException. 由于personlist
是泛型(参数化)类型,编译器会自动尝试将personlist.get(i)
转换为Person
对象,但由于它实际上是一个ArrayList,因此会得到ClassCastException。
The quickest way to fix this is to change the above-mentioned line in the write method to: 解决此问题的最快方法是将write方法中的上述行更改为:
List.addAll(s);
Then the list will contain Person objects instead of ArrayList objects. 然后列表将包含Person对象而不是ArrayList对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.