java.lang.String 不能转换为 [Ljava.lang.Object;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23651751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.String cannot be cast to [Ljava.lang.Object;
提问by user3627624
I want to call course name in combobox and print course Id which selected coursename How can I solve this problem?
我想在组合框中调用课程名称并打印选择课程名称的课程 ID 我该如何解决这个问题?
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] );
}
采纳答案by T.J. Crowder
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:
通过不尝试将 aString
转换为Object[]
. 查看您正在使用的方法的返回值,并使用适当类型的变量来存储这些返回值。 JComboBox#getSelectedItem
返回一个Object
(在这种情况下显然是 a String
),而不是一个数组(任何类型)。但在这一行:
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.
...您正试图将它转换为一个Object[]
(数组Object
),以便您可以将它存储在一个Object[]
. 你不能那样做。
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]
:
似乎row
应该只是Object
or String
, not Object[]
,并且当您使用它时,您应该直接使用它,而不是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?
我在组合框中调用了课程名称,但我应该将课程 ID 保存在我的数据库中。我如何获得课程 ID?
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:
我不知道JComboBox
。从根本上说,您需要存储包含两个值(ID 和名称)的内容,然后在获取所选项目时使用该内容。除非JComboBox
有一些内置的功能,否则您可能需要一个简单的类来保存值并toString
通过返回来实现courseName
。有点像:
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());
}
回答by Tim B
try:
尝试:
Object row = (Object)combocourse.getSelectedItem();
System.out.println("id"+row );
You only add single objects into the combocourse, not arrays of objects.
您只能将单个对象添加到组合课程中,而不是对象数组。
回答by user902383
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()
combocourse.getSelectedItem();
在您的情况下返回String
并且字符串不能转换为对象数组。如果你想获得对象列表,他们使用 getSelectedObjects()