1

I have marked the line thats giving me trouble

private void EditButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

DefaultListModel PatientListModel = new DefaultListModel();

for (Patient s : PatientList) {
    int AccNum = Integer.parseInt(IDTextField.getText());

    if (AccNum == s.getAccountNumber()) {

        s.setName(NameTextField.getText());
        s.setAge(Integer.parseInt(AgeTextField.getText()));
        s.setAddress(AddressTextField.getText());
        String PatientSex = "";

        if (MaleRadioButton.isSelected()) {
            PatientSex = "Male";
        }

        if (FemaleRadioButton.isSelected()) {
            PatientSex = "Female";
        }

        s.setSex(PatientSex);
        s.setPhone(PhoneTextField.getText());
        ArrayList<PatientCondition> PatientConditions3 = new ArrayList();
  ===>      PatientConditions3 = (ArrayList<PatientCondition>) ConditionsJList.getSelectedValuesList(); //error here
        s.setConditionsList(PatientConditions3);
        PatientInfoLabel2.setText("Patient Details Updated");

        for (Patient f : PatientList) {
            PatientListModel.addElement(f.getAccountNumber() + "-" + f.getName());
        }

        PatientJList.setModel(PatientListModel);
        UpdateAllViews();

        //       
    }
}
}                                 

the error is:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Collections$EmptyList cannot be cast to java.util.ArrayList
2
  • Nothing to do with the question but you shouldn't start your variable names with capitals, follow java guidelines it is very important. Commented Apr 12, 2013 at 11:18
  • @resus whilst yes it would be better to stick to the correct naming conventions. There is still nothing wrong with it that will stop the code from working. I would just advice the benny to have a read of this java.about.com/od/javasyntax/a/nameconventions.htm as using the correct naming convention will help you big time in the future when maintaining code. Even if you are just messing around with the code it would be good practice to start doing it. Commented Apr 12, 2013 at 11:25

6 Answers 6

5

I think the error message is pretty informative. You can't do that cast.

You should redefine your PatientConditions3 to be of type List<PatientCondition>. It is good practice to code against the interface List instead of a specific implementation, like ArrayList. Also, you should rename it to follow Java naming conventions:

List<PatientCondition> patientConditions3;

If you need to convert the received List to for example an ArrayList, you can create a new one with the elements of the received List:

patientConditions3 = new ArrayList<PatientCondition>(ConditionsJList.getSelectedValuesList());
Sign up to request clarification or add additional context in comments.

2 Comments

But I need to make it an arraylist to use it?
@benny: Added info on how to get an ArrayList from the received List.
1

Collections.emptyList() returns a List reference casting which to an ArrayList is illegal.

Try changing this

ArrayList<PatientCondition> PatientConditions3 = new ArrayList();

to this

List<PatientCondition> PatientConditions3 = new ArrayList();

1 Comment

How do I cast a list to arraylist to use it?
1

Most likely, the method declaration of the method ConditionsJList.getSelectedValuesList specifies that it returns a List. You cannot count on it that that list is going to be an ArrayList.

Change the type of your variable PatientConditions3 to List instead of ArrayList. Also, it is not necessary to create a new ArrayList and assign that to PatientConditions3 if you immediately assign to PatientConditions3 again; you then create that ArrayList for nothing, it's immediately thrown away.

List<PatientCondition> PatientConditions3 = ConditionsJList.getSelectedValuesList();

Another remark: According to common Java coding conventions, you shouldn't start variable names with an upper-case letter. Call it patientConditions3 instead of PatientConditions3. (And that applies ofcourse to all your other variables as well).

Comments

0

A List variable cannot be cast to an ArrayList if there is not actually a ArrayList behind it. In most cases you should rely on Interfaces for your variables to allow maximum flexibility

List<PatientCondition> PatientConditions3 = ConditionsJList.getSelectedValuesList(); 

Concerning type safety: Only cast if you are really sure the object to cast is an instance of the target type. check that with instanceof before you cast.

2 Comments

I need to cast it back to an arraylist though to use it?
if you change the type of PatientConditions3 to List<PatientCondition> then there is no need to cast. most of the methods ArrayList provides to client code are implementations of the List interface so in most cases List as type for variables is all you need
0

When you have there ConditionsJList.getSelectedValuesList(); you get a collection (or List<> if this sounds better to you) of some values. And you just need to cast them to be PatientCondition..So you should have there something like: PatientConditions3 = (PatientCondition) ConditionsJList.getSelectedValuesList();

Of course PatientConditions3 must be List<PatientCondition> ...

Comments

0

this does a cast for you if it's an ArrayList, otherwise the ArrayList is empty:

ArrayList<PatientCondition> patientConditions3 = (ConditionsJList.getSelectedValuesList() instanceof ArrayList<PatientCondition> ? ((ArrayList<PatientCondition>) ConditionsJList.getSelectedValuesList()) : new ArrayList<PatientCondition>());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.