gpt4 book ai didi

JavaFX 异常

转载 作者:行者123 更新时间:2023-12-02 05:44:39 40 4
gpt4 key购买 nike

我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台中收到此错误。真的很感谢一些解释这意味着什么..:)

jun 12, 2014 9:19:37 EM application.Main gotoLogin
SEVERE: null
javafx.fxml.LoadException:
/C:/Users/Administrator/Dropbox/Project%20BlueRift/bin/application/LoginGUI.fxml

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.replaceSceneContent(Main.java:53)
at application.Main.gotoLogin(Main.java:68)
at application.Main.start(Main.java:39)
at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.DBManager.connect(DBManager.java:54)
at controllers.LoginController.initialize(LoginController.java:48)
... 16 more

数据库连接:

package application;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.DatabaseMetaData;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

import controllers.IReportStatus;

public class DBManager {

private static DBManager INSTANCE;

private final String MYSQL_URL = "jdbc:mysql://213.65.171.115:3306";
private final String MYSQL_DATABASE = "/bluerift";
private final String MYSQL_USER = "test";
private final String MYSQL_PW = "test";
private String CURRENT_USER = null;
private String CURRENT_TIMELINE = null;

private Connection conn = null;
private Statement st = null;
private ResultSet rs = null;
private PreparedStatement preparedStatement = null;;
private DatabaseMetaData dbm = null;

private IReportStatus reportStatus;

private DBManager() {
}

public boolean connect() {
try {
if (conn != null) {
return true;
}

Class.forName("com.mysql.jdbc.Driver");

conn = (Connection) DriverManager.getConnection(MYSQL_URL
+ MYSQL_DATABASE, MYSQL_USER, MYSQL_PW);
st = (Statement) conn.createStatement();
dbm = (DatabaseMetaData) conn.getMetaData();
System.out.println("DevCon: Connected");
return true;
}

catch (Exception ex) {
reportStatus.setMsg("Could not reach database.");
conn = null;
st = null;
rs = null;
return false;
}
}

public boolean userLogin(String aUserName, String aPassword) {
aUserName = aUserName.trim();

if (aUserName.isEmpty()) {
reportStatus.setMsg("Empty Username");
return false;
}

if (aUserName.contains(" ")) {
reportStatus.setMsg("Username can not contain whitespaces");
return false;
}

if (aPassword.isEmpty()) {
reportStatus.setMsg("Empty Password");
return false;
}

try {
String query = "SELECT userName,userPassword FROM users";
rs = st.executeQuery(query);

while (rs.next()) {
if (rs.getString("userName").equals(aUserName)) {

if (rs.getString("userPassword").equals(aPassword)) {
System.out.println("DevCon: User "
+ MyUtil.quote(aUserName) + " logged in");
CURRENT_USER = aUserName;

return true;
}

else {
reportStatus.setMsg("Wrong Password");
return false;
}
}
}

reportStatus.setMsg("Username not found in database");
return false;
}

catch (Exception ex) {
reportStatus.setMsg("Could not reach database.");
conn = null;
st = null;
rs = null;
return false;
}
}

public boolean userRegister(String aName, String aPassword,
String confirmPassword, String aEmail) {

aEmail = aEmail.trim();
aName = aName.trim();

if (aName.length() < 5 || aName.length() > 25) {
reportStatus
.setMsg("Username has to be between 5 and 25 characters long");
return false;
}

if (!MyUtil.isAlphaNumericUs(aName)) {
reportStatus
.setMsg("Username can only contain a-Z, A-Z, 0-9 and _");
return false;
}

if (aEmail.isEmpty()) {
reportStatus.setMsg("Email can not be empty");
return false;
}

if (!MyUtil.isVaildEmail(aEmail)) {
reportStatus.setMsg("Email incorrect");
return false;
}

if (aPassword.length() < 5 || aPassword.length() > 30
|| confirmPassword.length() < 5
|| confirmPassword.length() > 30) {
reportStatus
.setMsg("Password must be between 5 and 30 characters long");
return false;
}

if (!aPassword.equals(confirmPassword)) {
reportStatus.setMsg("Password does not match");
return false;
}

try {
String query = "SELECT userName, userEmail FROM users";
rs = st.executeQuery(query);

while (rs.next()) {
if (aName.equals(rs.getString("userName"))) {
reportStatus.setMsg("Username already registred");
return false;
}

if (aEmail.equals(rs.getString("userEmail"))) {
reportStatus.setMsg("Email already registred");
return false;
}
}

query = "INSERT INTO users (userName, userPassword, userEmail)"
+ " VALUES (?, ?, ?)";

preparedStatement = (PreparedStatement) conn
.prepareStatement(query);
preparedStatement.setString(1, aName);
preparedStatement.setString(2, aPassword);
preparedStatement.setString(3, aEmail);
preparedStatement.execute();
}

catch (Exception ex) {
System.out.println("Error: " + ex);
return false;
}

return true;
}

public boolean createTimeline(String timelineName) {
timelineName = timelineName.trim();

if (timelineName.length() < 3 || timelineName.length() > 15) {
reportStatus
.setMsg("Name must be between 3 and 15 characters long");
return false;
}

System.out.println("timeline name in: " + timelineName);
if (!MyUtil.isAlphaNumeric(timelineName) || timelineName.isEmpty()) {
reportStatus
.setMsg("Invaild timeline name. Allowed symbols are a-z, A-Z and 0-9");
return false;
}

if (timelineExists(timelineName)) {

reportStatus.setMsg("Timeline called " + MyUtil.quote(timelineName)
+ " already exists");
return false;
}

else {

String query = "CREATE TABLE `bluerift`.`"
+ getCurrentUser()
+ "-"
+ timelineName
+ "` (`id` INT NOT NULL AUTO_INCREMENT,`eventName` VARCHAR(45) NULL,`eventYear` INT NULL,`eventDesc` LONGBLOB NULL,PRIMARY KEY (`id`));";

try {
st.executeUpdate(query);
} catch (SQLException e) {

e.printStackTrace();
}
}
return true;
}

public ObservableList<String> receiveTimelines() {
ObservableList<String> temp = FXCollections.observableArrayList();

try {
rs = dbm.getTables(null, null, "%", null);
while (rs.next()) {

if (rs.getString(3).contains(getCurrentUser() + "-")) {
temp.add(rs.getString(3).substring(
getCurrentUser().length() + 1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}

return temp;

}

private boolean timelineExists(String t) {

try {
rs = dbm.getTables(null, null, CURRENT_USER + "-" + t, null);

if (rs.next()) {
return true;
}
}

catch (SQLException e) {
e.printStackTrace();
}

return false;
}

public void deleteTimeline(String timelineName) {
try {
String query = "DROP TABLE `bluerift`.`" + getCurrentUser() + "-"
+ timelineName + "`;";
st.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
}
}

public String getCurrentUser() {
if (CURRENT_USER == null) {
return "DevCon: No user online";
}

return CURRENT_USER;
}

public void userLogout() {
if (CURRENT_USER != null) {
System.out.println("DevCon: User " + CURRENT_USER + " logged out");
CURRENT_USER = null;
}

else {
System.out.println("DevCon: No user online");
}
}

public void setReportTo(IReportStatus reportStatus) {
System.out.println("new ReportStatus is: " + reportStatus);
this.reportStatus = reportStatus;
}

public String getCurrentTimeline() {
return CURRENT_TIMELINE;
}

public static DBManager getInstance() {
if (INSTANCE == null) {
INSTANCE = new DBManager();
}
return INSTANCE;
}

}

我真的不知道是什么导致了空指针以及为什么

最佳答案

异常输出准确地指出了行号:

Caused by: java.lang.NullPointerException
at application.DBManager.connect(DBManager.java:54)

假设问题中的代码与您的生产代码相同,第 54 行包含:

reportStatus.setMsg("Could not reach database.");

reportStatus 可能未初始化,从而导致 NullPointerException

关于JavaFX 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24206480/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com