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] );
}
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.
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; .
lang. Object; is the name for Object[]. class , the java. lang. Class representing the class of array of Object .
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.
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:
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.
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.
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.
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());
}
try:
Object row = (Object)combocourse.getSelectedItem();
System.out.println("id"+row );
You only add single objects into the combocourse, not arrays of objects.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With