0

I made a JavaFX Application, and its GUI has these four following things:

  • A TextField, called name (takes in the username)
  • A PasswordField, called password (takes in the password)
  • A ToggleGroup, called gender (there's a method related to this ToggleGroup (male,female,other))
  • A TextField, called locationField (takes in the location name)

There is a sign up button at the bottom that takes all the data and saves it into the MySQL database

Here's the method related to the ToggleGroup

public String getGender() {
    String gen = "";
    if (male.isSelected()) {
        gen = "Male";
    } else if (female.isSelected()) {
        gen = "Female";
    } else if (other.isSelected()) {
        gen = "Other";
    }
    return gen;
}

Here's the eventhandler for the sign up button:

 @FXML
 public void signUP() throws SQLException{
    progress.setVisible(true);
    PauseTransition pt = new PauseTransition();
    pt.setDuration(Duration.seconds(3));
    pt.setOnFinished(e -> System.out.println("Sign Up Successful!"));
    pt.play();

String url = "jdbc:mysql://localhost:3306/genius";
    String user = "root";
    String password = "$Can$123";
    String sql = "insert into coders(names,password,gender,address)" + "values (?,?,?,?)";
    try {
        connection = DriverManager.getConnection(url, user, password);
        pst = connection.prepareStatement(sql);
        pst.setString(1, name.getText());
        pst.setString(2, password);
        pst.setString(3, getGender());
        pst.setString(4, locationField.getText());
        pst.executeUpdate();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

Here's the problem I'm facing: when I enter all the following details and then I click the signup button, it shows me this error:

java.lang.NullPointerException
at GaveUp/sample.SignUpController.signUP(SignUpController.java:83)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke
    (NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
    (DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
    (DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
    (CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
    (CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased
    (ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord
    .handleBubblingEvent (CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
    (CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
    (CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
    (BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
    (EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
    (GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
    (GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2
    (GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock
    (QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent
    (GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:835)

Under the NullPointerException, there's a line which says:

at GaveUp/sample.SignUpController.signUP(SignUpController.java:83)

which is reffering to the lines of the sign up button method which says:

pst.setString(1, name.getText());
pst.setString(3, getGender());
pst.setString(4, locationField.getText());

How should I fix this error. Any help would be appreciated, Thank you!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Which line exactly? My best guess is that: name or locationField are null. One or both of them. Try to debug your code. Finding Nullpointer is very easy. You can print out the variales or use the debugger. – Marcinek Apr 11 '20 at 00:43
  • Why are both of them null even though a user just entered data in it. And how should I debug, I'm using IntelliJ Idea as my ide, thank you – Ashmal Shoukat Apr 11 '20 at 05:00
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fabian Apr 11 '20 at 06:20
  • Iirc this is not the first time you post this question without providing sufficient information to differentiate this from the "standard duplication target" for this kind of exception. One thing you should check though is whether the controls are properly injected to the controller. (in particular `name` and `locationField` as they are the only candidates other than `pst` in the 3 lines that cause the exception that would yield this exact stacktrace.) This seems the only thing that could be causing the npe, since `prepareStatement` should throw an exception instead of returning `null` – fabian Apr 11 '20 at 06:23
  • So, what do you suggest I should do? Thank you. – Ashmal Shoukat Apr 11 '20 at 16:30

0 Answers0