Error entering user in java

0

I'm working on a project and now I need to create a view from the javafx side, which will be for new user registration but when I try to add it I get the error: "Exception in thread" JavaFX Application Thread "java.lang. RuntimeException: java.lang.reflect.InvocationTargetException ", from the side of SQLServer if I can add users but from Netbeans no, several times it has happened to me but this time I can not solve it, since this problem is due to misspelled names or the incompatibility of types but I already reviewed all that and that is not the problem, without more options to look for the error, I gave a debug to the project and I realized that there is a field where it tells me error

and in the other fields does not give an error

In theory the error says that it comes from the bean but I do not know why the properties of my bean are these:

private int codigoUsuario;
private String nombreUsuario;
private String email;
private String usuario;
private String contraseña;
private String tipoDeUsuario;

In this bean, I have all the getters and setters of the properties

Error message:

 Executing C:\Users\bryan\Documents\NetBeansProjects\SportStore2017090\dist\run582449146\SportStore2017090.jar using platform C:\Program Files\Java\jdk1.8.0_161\jre/bin/java
 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.jhonylopez.bean.Usuario
at org.jhonylopez.controller.RegistroController.agregar(RegistroController.java:130)
at org.jhonylopez.controller.RegistroController.nuevo(RegistroController.java:113)
... 58 more

RestrictorController Code:

public class RegistryController implements Initializable {

private enum operaciones {NUEVO, GUARDAR, CANCELAR, NINGUNO};
private operaciones tipoDeOperacion = operaciones.NINGUNO;
private ObservableList<String> listaTipoDeUsuario;
private ObservableList<Usuario> listaUsuario;
private Principal escenarioPrincipal;
private Boolean salida = false;

@FXML private TextField txtNombreUsuario;
@FXML private TextField txtEmailUsuario;
@FXML private TextField txtUsuario;
@FXML private TextField txtPassword;

@FXML private ComboBox cmbTipoUsuario;

@FXML private Button btnNuevo;
@FXML private Button btnCancelar;

@Override
public void initialize(URL location, ResourceBundle resources) {
    cmbTipoUsuario.setItems(getTipoDeUsuarios());
}

public ObservableList<Usuario> getUsuarios()
{
    ArrayList<Usuario> lista = new ArrayList<>();
    try
    {
        PreparedStatement procedimiento = Conexion.getInstancia().getConexion().prepareCall("{call sp_ListarUsuarios}");
        ResultSet resultado = procedimiento.executeQuery();
        while(resultado.next())
        {
            lista.add(new Usuario(resultado.getInt("codigoUsuario"), resultado.getString("nombreUsuario"),
                resultado.getString("email"), resultado.getString("usuario"), resultado.getString("contraseña"),
                resultado.getString("tipoDeUsuario")));
        }
    }catch(SQLException e)
    {
        e.printStackTrace();
        e.getMessage();
    }
    return listaUsuario = FXCollections.observableList(lista);
}

public ObservableList<String> getTipoDeUsuarios()
{
    ArrayList<String> lista = new ArrayList<>();

    lista.add("root");
    lista.add("admin");
    lista.add("sa");
    lista.add("invitado");

    return listaTipoDeUsuario = FXCollections.observableList(lista);
}

public void nuevo()
{
    switch(tipoDeOperacion)
    {
        case NINGUNO:
            activarControles();
            limpiarControles();
            btnNuevo.setText("Guardar");
            btnCancelar.setText("Cancelar");
            tipoDeOperacion = operaciones.GUARDAR;
            break;

        case GUARDAR:
            agregar();
            desactivarControles();
            btnNuevo.setText("Nuevo");
            btnCancelar.setText("Cancelar");
            tipoDeOperacion = operaciones.NINGUNO;
            limpiarControles();
            break;
    }
}

public void agregar()
{
    Usuario registro = new Usuario();
    registro.setNombreUsuario(txtNombreUsuario.getText());
    registro.setEmail(txtEmailUsuario.getText());
    registro.setUsuario(txtUsuario.getText());
    registro.setContraseña(txtPassword.getText());
    registro.setTipoDeUsuario(((Usuario)cmbTipoUsuario.getSelectionModel().getSelectedItem()).getTipoDeUsuario());
    try
    {
        PreparedStatement procedimiento = Conexion.getInstancia().getConexion().prepareCall("{call sp_AgregarUsuario(?,?,?,?,?)}");
        procedimiento.setString(1, registro.getNombreUsuario());
        procedimiento.setString(2, registro.getEmail());
        procedimiento.setString(3, registro.getUsuario());
        procedimiento.setString(4, registro.getContraseña());
        procedimiento.setString(5, registro.getTipoDeUsuario());
        procedimiento.execute();
        JOptionPane.showMessageDialog(null, "Usuario añadido");
        limpiarControles();

    }catch(SQLException e)
    {
        e.printStackTrace();
        e.getMessage();
        JOptionPane.showMessageDialog(null, "Error al ingresar usuario, por favor intentarlo otra vez");
    }
}

public void cancelar()
{
    switch(tipoDeOperacion)
    {
        case GUARDAR:
            desactivarControles();
            limpiarControles();
            btnNuevo.setText("Nuevo");
            tipoDeOperacion = operaciones.NINGUNO;
            break;
        default:            
    } 
}

public Usuario buscarUsuarios(int codigoUsuario)
{
    Usuario resultado = null;
    try
    {
        PreparedStatement procedimiento = Conexion.getInstancia().getConexion().prepareCall("{call sp_BuscarUsuario(?)}");
        procedimiento.setInt(1, codigoUsuario);
        ResultSet registro = procedimiento.executeQuery();
        while(registro.next())
        {
            resultado = new Usuario(registro.getInt("codigoUsuario"), registro.getString("nombreUsuario"),
                    registro.getString("email"), registro.getString("usuario"), registro.getString("contraseña"), 
                    registro.getString("tipoDeUsuario"));
        }

    }catch(SQLException e)
    {
        e.printStackTrace();
        e.getMessage();
    }
    return resultado;
}

public void limpiarControles()
{
    txtNombreUsuario.setText("");
    txtEmailUsuario.setText("");
    txtUsuario.setText("");
    txtPassword.setText("");

    cmbTipoUsuario.getSelectionModel().clearSelection();
}

public void desactivarControles()
{
    txtNombreUsuario.setEditable(false);
    txtEmailUsuario.setEditable(false);
    txtUsuario.setEditable(false);
    txtPassword.setEditable(false);

    cmbTipoUsuario.setDisable(true);
}

public void activarControles()
{
    txtNombreUsuario.setEditable(true);
    txtEmailUsuario.setEditable(true);
    txtUsuario.setEditable(true);
    txtPassword.setEditable(true);

    cmbTipoUsuario.setDisable(false);
}

public Principal getEscenarioPrincipal() {
    return escenarioPrincipal;
}

public void setEscenarioPrincipal(Principal escenarioPrincipal) {
    this.escenarioPrincipal = escenarioPrincipal;
}       

}
    
asked by Jhony López 19.05.2018 в 04:01
source

1 answer

1

The problem you're having is because the ComboBox cmbTipoUsuario contains objects of type String and you're trying to cast it to Usuario :

 (Usuario) cmbTipoUsuario.getSelectionModel().getSelectedItem()

This causes the java.lang.ClassCastException exception to be thrown, which is the cause of the error.

If you look at the end of the trace of the error, it tells you clearly:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.jhonylopez.bean.Usuario

To fix it, eliminate the casting to Usuario , change it to a String and remove the call to the getTipoDeUsuario() method, like this:

registro.setTipoDeUsuario((String) cmbTipoUsuario.getSelectionModel().getSelectedItem());
    
answered by 19.05.2018 / 04:48
source