Error when instantiating object

Asked

Viewed 49 times

0

in java fx, when I click the button, it should run:

private void btnLoginClick(ActionEvent event) {
    String name =  txtUser.getText();
    String password = txtPassword.getText();
    User user = new User(name , password);

    if(name == "felipe"){
        System.out.println("Logado");
    } else{
        System.out.println("ERRO AO LOGAR");
    }
}

But it gives error when instantiating the object.

The mistake is huge, I’ll post the beginning of it:

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:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
  • The given error is caused by a method invocation via reflection. Has not nothingness of your call stack code (stack trace). Should have a lower part with a text caused by, check?

  • 1

    Taking advantage: name == "felipe" will not give true in your code. That’s because the operator == when applied to objects checks if the instance is the same, but the creation of the string through the getText() will generate (quite possibly) a new string from the string saved in the component. To compare object contents you must use the method equals, then the right thing would be "felipe".equals(nome)

  • @Jeffersonquesado thanks for the help. You’re right (Caused by: java.lang.reflect.Invocationtargetexception).. As for the equals I had discovered later, when to all you talked about the Reflection and such, I really have no idea what you’re talking about, I’m new. I will study more to understand all this and find a solution. Thanks (y)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.