这是我与日志一起使用的代码,由于我对使用数据库还很陌生,所以我不确定为什么会这样。 我已经提供了登录验证和getConnection()方法,因为我确定哪里出错了。感谢所有帮助。预先谢谢你。
public void validateLogin(){
DatabaseConnection connectNow = new DatabaseConnection();
Connection connectDB = connectNow.getConnection();
String verifyLogin = "SELECT count(1) FROM user_account WHERE username = '" + usernameTextField.getText() + "' AND password ='" + passwordEnterField.getText() + "'";
try{
Statement statement = connectDB.createStatement();
ResultSet queryResult = statement.executeQuery(verifyLogin);
while (queryResult.next()){
if (queryResult.getInt(1) == 1){
loginMessage.setText("Login Successful");
}else {
loginMessage.setText("Invalid Login, try again");
}
}
}catch (Exception e){
e.printStackTrace();
e.getCause();
}
}
getConnection()方法:
public Connection getConnection(){
String databaseName = "accounts";
String databaseUser = "username";
String databasePassword = "password";
String url = "jdbc:mysql://localhost/" + databaseName;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
databaseLink = DriverManager.getConnection(url,databaseUser,databasePassword);
}catch (Exception e){
e.getStackTrace();
e.getCause();
}
return databaseLink;
}
日志: java.lang.NullPointerException:无法调用“ java.sql.Connection.createStatement()”,因为“ connectDB”为空 在Zimsy / demonic.LoginController.validateLogin(LoginController.java:67) 在Zimsy / demonic.LoginController.loginButtonAction(LoginController.java:49) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) 在jdk.internal.reflect.GeneratedMethodAccessor2.invoke(未知来源) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在javafx.base / com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) 在javafx.fxml / com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) 在javafx.fxml / javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1782) 在javafx.fxml / javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1670) 在javafx.base / com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在javafx.base / com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在javafx.base / com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在javafx.base / javafx.event.Event.fireEvent(Event.java:198) 在javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879) 在javafx.controls / javafx.scene.control.Button.fire(Button.java:200) 在javafx.controls / com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206) 在javafx.controls / com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) 在javafx.base / com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在javafx.base / com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在javafx.base / com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在javafx.base / com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在javafx.base / com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在javafx.base / javafx.event.Event.fireEvent(Event.java:198) 在javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851) 在javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579) 在javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) 在javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588) 在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397) 在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 在java.base / java.security.AccessController.doPrivileged(AccessController.java:391) 在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434) 在javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390) 在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433) 在javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556) 在javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942) 在javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) 在java.base / java.lang.Thread.run(Thread.java:832)