benny
benny

Reputation: 13

Java Arraylist/List error

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

Upvotes: 1

Views: 8850

Answers (6)

bapho
bapho

Reputation: 936

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

Upvotes: 0

Keppil
Keppil

Reputation: 46209

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

Upvotes: 5

Martin Dvoracek
Martin Dvoracek

Reputation: 1738

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> ...

Upvotes: 0

Jesper
Jesper

Reputation: 206796

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).

Upvotes: 1

Marco Forberg
Marco Forberg

Reputation: 2644

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.

Upvotes: 0

user2030471
user2030471

Reputation:

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

Upvotes: 1

Related Questions