忙于学校项目,NetBeans让我感到沮丧。请帮我解决以下错误,请告诉我我做错了什么。
我忙于绑定和Apache Derby。我正在做的是我将数据库中的数据绑定到文本字段。我有一个带有两个标签面板的JFrame表单。一个人有一个表格1,另一个表格显示个人记录2。
这是单个记录视图上绑定编码的摘录:
1 package UserManager;
import java.util.*;
5 /**
*
* @author Johan Brink
*/
public class Data extends javax.swing.JFrame {
10 Users users = new Users();
ListIterator<Users>iterator;
public Users getUsers() {
return users;
15 }
public void setUsers(Users users) {
Users oldUsers = this.users;
this.users = users;
20 firePropertyChange("users", oldUsers, users);
}
/**
* Creates new form Data
25 */
public Data() {
initComponents();
setLocationRelativeTo(rootPane);
iterator = usersList.listIterator(0);
30 setUsers(iterator.next());
}
问题在于第29和30行。这在过去有效,但现在我收到了错误。我有一个自动创建的实体类等。没有第29和30行,一切都有效,但未列出个别记录信息。插入第29和30行时,运行时会显示以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Cannot cast java.util.Date to java.lang.String
at java.lang.Class.cast(Class.java:3084)
at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
at org.jdesktop.beansbinding.AutoBinding.sourceChangedImpl(AutoBinding.java:227)
at org.jdesktop.beansbinding.Binding.sourceChanged(Binding.java:1411)
at org.jdesktop.beansbinding.Binding.access$1200(Binding.java:38)
at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged(Binding.java:1618)
at org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange(PropertyHelper.java:212)
at org.jdesktop.beansbinding.ELProperty.notifyListeners(ELProperty.java:688)
at org.jdesktop.beansbinding.ELProperty.access$800(ELProperty.java:155)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.processSourceChanged(ELProperty.java:312)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.sourceChanged(ELProperty.java:326)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.propertyChange(ELProperty.java:333)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.Component.firePropertyChange(Component.java:8382)
at UserManager.Data.setUsers(Data.java:24)
at UserManager.Data.<init>(Data.java:34)
at UserManager.Data$7.run(Data.java:419)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:1)
你没有包含作为根问题的代码,但是例外情况说绑定代码试图将String
转换为Date
...这是一个很大的禁忌用Java!
我怀疑问题是您在User
对象中有一个类型为Date
的出生日期字段,并且绑定对象正在尝试反射性地将String
分配给它
如果这是问题,那么您需要将自定义Converter
添加到相关Binding
对象,以便在String
和Date
对象之间进行转换。看起来你需要编写一个自定义转换器来完成这项工作。
参考文献: