java.lang.ClassCastException:java.lang.String无法强制转换为Panels.AddNewClientSaveAction

时间:2013-08-13 07:01:17

标签: java arraylist

运行下面的代码时出现以下错误: 运行:

java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction

整个错误:

run:
java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction
Before
    at Database.FileUpdate.main(128 | Oli | Much
FileUpdate.java:40)
128 | Oli | Much
128 | Francis Kariuki | Mahia
128 | Francis Kariuki | Mahia
LASTSTUDENTEENTRYLINENNAMES
BUILD SUCCESSFUL (total time: 4 seconds)

我正在尝试创建一个功能,在特定文本之前将新用户添加到文本文件中,在这种情况下为“LASTSTUDENTEENTRYLINENNAMES”。

以下是添加新用户“Eddys Rockery”之前和之后的示例。

在:

123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES

后:

123 | Oliver | Muchai
456 | Revilo | Chamu
678 | Eddys | Rockery
LASTSTUDENTEENTRYLINENNAMES

到目前为止的守则。我已经指出了我认为错误是从哪里生成的。

提前感谢大家的任何帮助和建议。

import Panels.AddNewClientSaveAction;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Stack {

    public static final String LAST_STUDENT_LINE = "LASTSTUDENTEENTRYLINENNAMES";
    public static StringBuilder line;

    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder(128);
        List<AddNewClientSaveAction> objectInputFieldsList = new ArrayList<>(25);

        AddNewClientSaveAction values = new AddNewClientSaveAction();
        objectInputFieldsList.addAll(values.rayArrayList());

        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader("/D:/TestFile.DAT/"));
            String text = null;
            while ((text = br.readLine()) != null) {
                if (sb.length() > 0) {
                    sb.append("\n");
                }
                sb.append(text);
            }

            System.out.println("Before");
            System.out.println(sb);

            // The Error's here: for (AddNewClientSaveAction s : objectInputFieldsList)
            for (AddNewClientSaveAction s : objectInputFieldsList) {

                int insertIndex = sb.indexOf(LAST_STUDENT_LINE);
                line = new StringBuilder(128);
                line.append(s.objectGUID).append(" | ").append(s.userGUID).append("\n");
                sb.insert(insertIndex, line.toString());
            }

            System.out.println("\nAfter");
            System.out.println(sb);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException exp) {
            }
        }


        try{
            java.io.FileWriter fstream = new java.io.FileWriter("/D:/TestFile.DAT/");
            BufferedWriter outobj = new BufferedWriter(fstream);
            outobj.write(sb.toString() + "\n");
            outobj.close();

        }catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

    }
}

另一类:

import Panels.AddNewClient;
import java.util.ArrayList;

public class StackAddClientSaveAction {
    final public ArrayList objectInputFieldsList = new ArrayList();

    public String objectGUID;
    public String userGUID;
    public String firmGUID;
    public String postalCode;

    public ArrayList rayArrayList () {
        StackAddClientSaveAction addNewClientSaveAction = new StackAddClientSaveAction();
        return addNewClientSaveAction.actionPerformed();
    }

    public ArrayList actionPerformed ()
    {
        // AddNewClient Class prints out the GUI where postalCode is entered via JTextField
        AddNewClient addNewClient = new AddNewClient();

        objectGUID = "1452";
        userGUID = "90378";
        firmGUID = "3663287";
        postalCode = addNewClient.postalCodeJTextField.getText();

        // Add to list
        objectInputFieldsList.add(objectGUID);
        objectInputFieldsList.add(userGUID);
        objectInputFieldsList.add(firmGUID);
        objectInputFieldsList.add(postalCode);

        return objectInputFieldsList;
    }
}

2 个答案:

答案 0 :(得分:1)

在数组列表objectInputFields List中,您将在以下方法中添加String

public ArrayList actionPerformed () {

    .....
    .....

        objectGUID = "1452";
        userGUID = "90378";
        firmGUID = "3663287";
    .....
    .....

        // Add to list
        objectInputFieldsList.add(objectGUID);
        objectInputFieldsList.add(userGUID);
        objectInputFieldsList.add(firmGUID);
        objectInputFieldsList.add(postalCode);
    .....
    .....
}

并且您正在尝试使用AddNewClientSaveAction类循环,这肯定会导致java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction错误。

    public static void main(String[] args) {

        .......
        .......

            // The Error's here: for (AddNewClientSaveAction s : objectInputFieldsList)
            for (AddNewClientSaveAction s : objectInputFieldsList) {

                int insertIndex = sb.indexOf(LAST_STUDENT_LINE);
                line = new StringBuilder(128);
                line.append(s.objectGUID).append(" | ").append(s.userGUID).append("\n");
                sb.insert(insertIndex, line.toString());
            }
        .......
        .......
   }

答案 1 :(得分:1)

方法StackAddClientSaveAction.rayArrayList()返回包含ArrayList个对象的原始String

在您的班级Stack中,您将String个对象的列表添加到objectInputFieldsList,这个列表应该包含AddNewClientSaveAction个对象,而不是String对象。当您尝试编译时,编译器会发出警告;你忽略了警告。

然后,您循环遍历objectInputFieldsList,并尝试将其包含的值分配给sAddNewClientSaveAction类型:

for (AddNewClientSaveAction s : objectInputFieldsList) {

您获得ClassCastException,因为该列表包含String个对象,而不是AddNewClientSaveAction个对象。

解决方案:不要将String个对象添加到列表中。不要忽略编译器警告;他们是有原因的。