Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I get the following error when I run the Code below: run:

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

The whole error:

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)

I'm trying to to create a functionality that has a new User added into a text file before a particular text, "LASTSTUDENTEENTRYLINENNAMES" in this case.

Here is an example of the before and after a new user "Eddys Rockery" is added.

Before:

123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES

After:

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

The Code so far. I've indicated where I think the error's being generated from.

Thank you all in advance for any help and suggestions.

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());
        }

    }
}

Another Class:

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;
    }
}
share|improve this question
    
Please always post the stack trace and not just the error message. It identifies a specific line where the problem is, and if you'd note that line in your copy/paste, you'll be much more likely to get a helpful response. Also, the problem appears to be that you have heap pollution from adding Strings to your objectInputFieldsList, but you haven't posted code for AddNewClientSaveAction#rayArrayList(). – chrylis Aug 13 '13 at 7:02

In your array list objectInputFields List you are adding String in the below method

public ArrayList actionPerformed () {

    .....
    .....

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

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

and you are trying to loop with AddNewClientSaveAction class, which will definitely cause java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction error.

    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());
            }
        .......
        .......
   }
share|improve this answer
    
I'm a JAVA newbie. Could you please add some insight on how I can fix that? – Oliver Muchai Aug 13 '13 at 7:15

The method StackAddClientSaveAction.rayArrayList() returns a raw ArrayList that contains String objects.

In your class Stack you're adding that list with String objects to objectInputFieldsList, a list that is supposed to contain AddNewClientSaveAction objects instead of String objects. The compiler gives you a warning when you try to compile that; you ignored the warning.

Then you loop over objectInputFieldsList, and try to assign the values it contains to s, which is of type AddNewClientSaveAction:

for (AddNewClientSaveAction s : objectInputFieldsList) {

You get a ClassCastException because the list contains String objects, not AddNewClientSaveAction objects.

Solution: Don't add String objects to the list. Don't ignore compiler warnings; they're there for a reason.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.