2013-01-18 204 views
-2

我工作JComboBox es和DefaultComboboxModel我填寫了我的數據庫。現在我想在我的數據庫中保存Enregistrement。但我收到此錯誤信息錯誤java.lang.String不能轉換爲metier.Motif

java.lang.String cannot be cast to metier.Motif 

這是我的班基序:

package metier; 


public class Motif { 

private Integer id; 
private String libelle; 
private Boolean recetteDepense; 
Enregistrement enregistrements[]; 

public Motif() { 
    this.id = null; 
    this.libelle = ""; 
    this.recetteDepense = null; 
} 

public Motif(Integer id, String libelle, Boolean recetteDepense, Enregistrement[] enregistrements) { 
    this.id = id; 
    this.libelle = libelle; 
    this.recetteDepense = recetteDepense; 
    this.enregistrements = enregistrements; 
} 

public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

public String getLibelle() { 
    return libelle; 
} 

public void setLibelle(String libelle) { 
    this.libelle = libelle; 
} 

public Boolean getRecetteDepense() { 
    return recetteDepense; 
} 

public void setRecetteDepense(Boolean recetteDepense) { 
    this.recetteDepense = recetteDepense; 
} 

public Enregistrement[] getEnregistrement() { 
    return enregistrements; 
} 

public void setEnregistrement(Enregistrement[] enregistrements) { 
    this.enregistrements = enregistrements; 
} 

@Override 
public String toString() { 
    return "Motif{" + "id=" + id + ", libelle=" + libelle + ", recetteDepense=" + recetteDepense + ", enregistrements=" + enregistrements + '}'; 
} 



} 

這是我的函數保存Enregistrement

public void enregistrerEnregistrement() throws DaoException { 
    // Déclarations de variables locales 
    dao = new DaoH2("gestComptes", "sa", ""); 
    try { 
     dao.connecter(); 

     Motif motif = (Motif)(((VueAjouterEnregistrement)vue).getModeleJComboBoxMotif().getSelectedItem());    
     System.out.print(motif.getId()); 

     //dao.ajouterUnEnregistrement(id, idLibelle, idModeReglement, idCompte, idEtat, idMotif, PreLEnc, RecDep, DateEnr, montant, ancienSolde, nouveauSolde, dateFacture, numCHQ, anticipation); 
    } catch (DaoException ex) { 
     JOptionPane.showMessageDialog(vue, "CtrlAjouterEnregistrement - instanciation - " + ex.getMessage(), "Enregistrement", JOptionPane.ERROR_MESSAGE); 
    } 

    } 

Combobox ES工作,但我不不知道如何用我的對象Motif的獲得者和設置者恢復元素。

我得到這個錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to metier.Motif 
at controleurs.CtrlAjouterEnregistrement.enregistrerEnregistrement(CtrlAjouterEnregistrement.java:235) 
at vues.VueAjouterEnregistrement.jButtonValiderActionPerformed(VueAjouterEnregistrement.java:1288) 
at vues.VueAjouterEnregistrement.access$000(VueAjouterEnregistrement.java:31) 
at vues.VueAjouterEnregistrement$1.actionPerformed(VueAjouterEnregistrement.java:187) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6270) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
at java.awt.Container.dispatchEventImpl(Container.java:2273) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:682) 
at java.awt.EventQueue$3.run(EventQueue.java:680) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:696) 
at java.awt.EventQueue$4.run(EventQueue.java:694) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 
+1

在哪一行,您是否看到異常?你能提供堆棧跟蹤嗎? – Swapnil

+0

顯示請填寫完整的異常跟蹤! – Andremoniy

回答

3

在這一行

Motif motif = (Motif)(((VueAjouterEnregistrement)vue) 
        .getModeleJComboBoxMotif().getSelectedItem()); 

你asume是getSelectedItem()地方實際上它返回一個String返回Motif

您不應該將字符串添加到組合框中,而是Motifs,並且Motif實現toString要在GUI中正確表示。然後getSelectedItem方法將爲您提供選定的Motif。

+0

我已經找到了對你的好處!事實上,在我的jcombobox中,我添加了值而不是對象。 Thx併爲這個愚蠢的錯誤感到抱歉! –

2

在這一行:

Motif motif = (Motif)(((VueAjouterEnregistrement)vue).getModeleJComboBoxMotif().getSelectedItem()); 

你想投一個字符串(由(((VueAjouterEnregistrement)vue).getModeleJComboBoxMotif().getSelectedItem())返回)

Motif

這是不可能的。

您可以將一個構造函數添加到Motif,它將String作爲參數,然後調用該構造函數。

編輯

您的評論,你問我如何調用構造函數。恕我直言,你應該閱讀一本Java書或在線教程,因爲調用構造函數並不神奇。這是基本的Java。

你可以這樣做:

String motifString = ((VueAjouterEnregistrement)vue).getModeleJComboBoxMotif().getSelectedItem(); 
Motif motif = new Motif(motifString); 
+0

我創建了我的構造函數,但是如何在我的控制器中調用它? –

+0

看到我的編輯.... – jlordo

+0

Thx和我知道如何調用構造函數,但此字符串爲null。不過,我在我的組合框中有數據。 –

相關問題