Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.String cannot be cast to [Ljava.lang.Object;

I want to call course name in combobox and print course Id which selected coursename How can I solve this problem?

    public void coursename(){
     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
     session.beginTransaction();
     Query query= session.createQuery("select a.courseName,a.courseId  from Semester e inner join e.course as a"); 
   for (Iterator it = query.iterate(); it.hasNext();) {
      Object  row[] = (Object[])   it.next();
      combocourse.addItem(row[0]);
        }        
       session.close();
   }


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


  JComboBox combocourse = (JComboBox)evt.getSource();  
   Object row[] = (Object[])combocourse.getSelectedItem();  
    System.out.println("id"+row[1] ); 

       }
like image 456
user3627624 Avatar asked May 14 '14 10:05

user3627624


People also ask

How do I resolve Java Lang ClassCastException error?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What is class Ljava Lang String?

It's just what the toString method of a JVM array returns. The default implementation of toString that gets inherited from java.lang.Object is more or less equal to this: def toString(): String = this.getClass.getName + "@" + this.hashCode.toHexString. The class name of a String array is [Ljava. lang. String; .

What does Ljava Lang object mean?

lang. Object; is the name for Object[]. class , the java. lang. Class representing the class of array of Object .

What is Java Lang ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

Why can't I cast a string to an object?

By not trying to cast a String to an Object []. Look at the return value of the methods you're using, and use variables typed appropriately to store those return values. JComboBox#getSelectedItem returns an Object (in this case apparently a String ), not an array (of any kind). But in this line:

Can we cast a string to a string array in Java?

Introduction Of course, we'd never suppose that we can cast a String to a String array in Java: But, this turns out to be a common JPA error.

How do I fix list casting error in Java?

One simple way to fix it, could be to rely on the raw type for the result (it is not the most elegant approach but the simplest one) then later you can check the type of the content of the list to cast it properly.

What is ClassCastException in Java and how to fix it?

ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion feature and data type conversion is successful only where a class extends a parent class and the child class is cast to its parent class.


3 Answers

By not trying to cast a String to an Object[]. Look at the return value of the methods you're using, and use variables typed appropriately to store those return values. JComboBox#getSelectedItem returns an Object (in this case apparently a String), not an array (of any kind). But in this line:

Object row[] = (Object[])combocourse.getSelectedItem();

...you're trying to cast it to be an Object[] (array of Object) so you can store it in an Object[]. You can't do that.

It seems like row should just be Object or String, not Object[], and that when you use it you should just use it directly, not as row[1]:

Object row = combocourse.getSelectedItem();  
System.out.println("id"+row ); 

Or

String row = (String)combocourse.getSelectedItem();  
System.out.println("id"+row ); 

In a comment you asked:

I called coursename in combobox but i should save course id in my database. How can I get courseId?

I don't know JComboBox. Fundamentally, you need to store something that contains both values (the ID and name) and then use that something when you get the selected item. Unless JComboBox has some functionality for this built in, you might need a simple class for that that holds the values and implements toString by returning courseName. Something vaguely like:

class CourseItem {
    private String courseName;
    private String courseId; // Or int or whatever

    CourseItem(String courseName,String courseId) {
        this.courseName = courseName;
        this.courseId = courseId;
    }

    public String getCourseName() {
        return this.courseName;
    }

    public String getCourseId() {
        return this.courseId;
    }

    public String toString() { // For display
        return this.courseName;
    }
}

Then:

public void coursename() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery("select a.courseName,a.courseId  from Semester e inner join e.course as a");
    for (Iterator it = query.iterate(); it.hasNext();) {
        Object row[] = (Object[]) it.next();
        combocourse.addItem(new CourseItem((String)row[0], (String)row[1]));
    }
    session.close();
}

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


    JComboBox combocourse = (JComboBox) evt.getSource();
    CourseItem item = (CourseItem)combocourse.getSelectedItem();
    System.out.println("id" + item.getCourseId());

}
like image 52
T.J. Crowder Avatar answered Oct 07 '22 22:10

T.J. Crowder


try:

Object row = (Object)combocourse.getSelectedItem();  
System.out.println("id"+row ); 

You only add single objects into the combocourse, not arrays of objects.

like image 1
Tim B Avatar answered Oct 07 '22 20:10

Tim B


combocourse.getSelectedItem(); in your case returns String and string cannot be cast to array of objects. If you want to get List of Objects, they use getSelectedObjects()

like image 1
user902383 Avatar answered Oct 07 '22 20:10

user902383