简体   繁体   English

线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:java.util.ArrayList

[英]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.

相关问题 线程“ AWT-EventQueue-0”中的java异常java.lang.ClassCastException - java Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 错误:线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException: - Error :Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException错误 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException error 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:add()异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: Exception with add() 我的JFrame类中的线程“AWT-EventQueue-0”java.lang.ClassCastException中的异常 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException in my JFrame class 线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:javax.swing.JTable - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: javax.swing.JTable Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String java.lang.ClassCastException:java.util.ArrayList - java.lang.ClassCastException: java.util.ArrayList Java:线程“ AWT-EventQueue-0”中的异常java.util.ConcurrentModificationException - Java: Exception in thread “AWT-EventQueue-0” java.util.ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM