Wayne Daly
Wayne Daly

Reputation: 55

Java Casting Object to Type?

I want to select the items in the JList specific to what the patient already clicked. If I click on the 3rd patient, the JList will select the items that patient selected.

In the PatientConList are the condition objects for that patient.

When you add a patient you do this:

List PatientConditions1 = new ArrayList();
PatientConditions1 =  ConditionsJList.getSelectedValuesList();

Then when you click on a patient it does this

public void MakePatientJListSelected(ArrayList PatientConList) {
    //this makes selected, so get condition array off patient.
    //so call this from patient jlist listener
    ConditionsJList.clearSelection();
    //get number - 1, so then 0 = first
    ArrayList<Conditions> PassedIn = PatientConList;
    ArrayList SelectList = new ArrayList();

    for(Conditions p: PassedIn)
    {
       int Num = p.getNumber();
       Num = Num - 1;
       SelectList.add(Num);
    }    

    //int startSel = 0;
    //int endSel = 0;
    //ConditionsJList.setSelectionInterval(startSel, endSel);

    for(int k=0;k<PassedIn.size();k++)
    {
        int startSel2 = k;
        int endSel2 = k;
        ConditionsJList.addSelectionInterval(startSel2, endSel2);
    }    
}

Sorry, Basically I have a JList and you can select the conditions you want and click add. this adds the list of conditions to the patient.

Basically when you select a patient to view I want it so that the jlist conditions for that patient are selected on the jlist.

Im getting this error Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to DentistAppNew.Conditions

My patient class:

import java.util.*;

public class Patient {

private int AccountNumber;
private String Name;
private int Age;
private String Address;
private String  Sex;
private String Phone;
private List<Conditions> ConditionNames;

public Patient( int AccountNumber1,
                String Name1, 
                int Age1, 
                String Address1, 
                String Sex1, 
                String Phone1, 
                List<Conditions> ConditionNames1)
{                
    this.AccountNumber = AccountNumber1;
    this.Name = Name1;
    this.Age = Age1;
    this.Address = Address1;
    this.Sex = Sex1;
    this.Phone = Phone1;
    this.ConditionNames = ConditionNames1
}

public  int getAccountNumber() { return AccountNumber; }

public  String getName() { return Name; }

public int getAge() { return Age; }

public String getAddress() { return Address; }

public String getSex() { return Sex; }

public String getPhone() { return Phone; }

public List getConditionsList() { return ConditionNames; }

public  void setName(String Name1) { this.Name = Name1; }

public void setAge(int Age1) { this.Age = Age1; }

public void setAddress(String Address1) { this.Address = Address1; }

public void setSex(String Sex1) { this.Sex = Sex1; }

public void setPhone(String Phone1) { this.Phone = Phone1; }

}

my conditions class:

public class Conditions {

    private int Number;
    private String Name;

    public Conditions( int Number1,String Name1) {
        this.Number = Number1;
        this.Name = Name1;      
    }   

    public  int getNumber() {
        return Number;
    }

    public  String getName() {
        return Name;
    }   
}

Upvotes: 0

Views: 218

Answers (2)

Raunak Agarwal
Raunak Agarwal

Reputation: 7228

Your method takes the input ArrayList you have to down cast it

ArrayList<Conditions> PassedIn = (ArrayList<Conditions>)PatientConList;

Upvotes: 2

Alessandro Vermeulen
Alessandro Vermeulen

Reputation: 1331

I suspect that the list your are getting passed is a list of strings, and not a list of Conditions as you assume here:

for(Conditions p: PassedIn)

However, you are giving us very little to go on. What is the full error message?

Also, try changing the type of the MakePatientJListSelected to ArrayList<Condition>. I think you will find that the compiler will tell you that it expects an ArrayList<String> there.

Upvotes: 2

Related Questions