castear alumno
Publicado por Charly (118 intervenciones) el 08/02/2017 13:40:14
Hola tengo un combobox y quiero que al seleccionar un objeto me lo convierta a la clase Alumno que tengo creada.
He probado a convertirlo primero a string y de ahí a compararlo con el resto de strings del archivo donde se guardan pero no hay manera.
Me dice que no puedo castear de String a Alumno, ni tampoco de Object a Alumno.
El método donde lo tengo es este:
Y el mensaje de error es el siguiente:
java.lang.ClassCastException: java.lang.String cannot be cast to Alumno
at gestorAlumnos.mostrar_registro_bajas_por_clave(gestorAlumnos.java:422)
at gestorAlumnos.actionPerformed(gestorAlumnos.java:526)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.addElement(Unknown Source)
at javax.swing.JComboBox.addItem(Unknown Source)
at gestorAlumnos.actualizar_combo(gestorAlumnos.java:393)
at gestorAlumnos.stateChanged(gestorAlumnos.java:487)
at javax.swing.JTabbedPane.fireStateChanged(Unknown Source)
at javax.swing.JTabbedPane$ModelListener.stateChanged(Unknown Source)
at javax.swing.DefaultSingleSelectionModel.fireStateChanged(Unknown Source)
at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(Unknown Source)
at javax.swing.JTabbedPane.setSelectedIndexImpl(Unknown Source)
at javax.swing.JTabbedPane.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
El error me lo marca en la línea que pone: a=(Alumno)combo_bajas.getSelectedItem();
He probado a convertirlo primero a string y de ahí a compararlo con el resto de strings del archivo donde se guardan pero no hay manera.
Me dice que no puedo castear de String a Alumno, ni tampoco de Object a Alumno.
El método donde lo tengo es este:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private void mostrar_registro_bajas_por_clave(String clave){
if(clave!=null){
try{
Object o=new Alumno(clave);
Alumno a=(Alumno)o;
a=(Alumno)combo_bajas.getSelectedItem();
txt_clave_bajas.setText(a.getClave());
txt_nombre_bajas.setText(a.getNombre());
txt_apellidos_bajas.setText(a.getApellidos());
txt_edad_bajas.setText(a.getEdad());
txt_calle_bajas.setText(a.getDireccion().getCalle());
txt_numero_bajas.setText(a.getDireccion().getNumero());
txt_codpostal_bajas.setText(a.getDireccion().getCodPostal());
}catch(Exception e){
e.printStackTrace();
}
}else{
limpiar_cajas_bajas();
}
}
java.lang.ClassCastException: java.lang.String cannot be cast to Alumno
at gestorAlumnos.mostrar_registro_bajas_por_clave(gestorAlumnos.java:422)
at gestorAlumnos.actionPerformed(gestorAlumnos.java:526)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.addElement(Unknown Source)
at javax.swing.JComboBox.addItem(Unknown Source)
at gestorAlumnos.actualizar_combo(gestorAlumnos.java:393)
at gestorAlumnos.stateChanged(gestorAlumnos.java:487)
at javax.swing.JTabbedPane.fireStateChanged(Unknown Source)
at javax.swing.JTabbedPane$ModelListener.stateChanged(Unknown Source)
at javax.swing.DefaultSingleSelectionModel.fireStateChanged(Unknown Source)
at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(Unknown Source)
at javax.swing.JTabbedPane.setSelectedIndexImpl(Unknown Source)
at javax.swing.JTabbedPane.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
El error me lo marca en la línea que pone: a=(Alumno)combo_bajas.getSelectedItem();
Valora esta pregunta


0