【问题标题】:Java FX Exception in Application start method cant start program应用程序启动方法中的 Javafx 异常无法启动程序
【发布时间】:2015-01-11 18:26:26
【问题描述】:

我正在尝试从数据库中获取一列并将其提供给组合框。它不工作。我看到了类似错误的其他答案,但这似乎不是我删除 poplist 方法的情况,它工作正常。不知何故,它无法将结果集放入 lst。我的 fxml 在正确的位置。这是我的代码。 Testfx.java

package testfx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Testfx extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("myfxml.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}
public static void main(String[] args) {
    launch(args);
}

}`

myfxmlController.java

package testfx;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.ObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Cell;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
import javax.swing.ComboBoxModel;

public class myfxmlController implements Initializable {
        public String dbUrl = "jdbc:oracle:thin:@exa1-scan:1521/....ORG";
    public Connection connection = null;
    public Statement statement = null;
    public PreparedStatement prstatement = null;
    public ResultSet rs = null;
    public String query = null;
    private ArrayList<String> lst = null;
    public String gammat = null;
    @FXML
    private Label label;
    @FXML
    private ComboBox combobox;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
        try {
            // TODO
            connection = DriverManager.getConnection("jdbc:derby:c:\\Mydb", "root", "asdf");

            if(connection != null)
            {
            System.out.println("Database Connected!");
            System.out.println(connection);
            }

        } catch (SQLException ex) {
            Logger.getLogger(myfxmlController.class.getName()).log(Level.SEVERE, null, ex);
        }
        poplist();
        ObservableList lists = FXCollections.observableArrayList(lst);


        combobox.setItems(lists);


} 
public void poplist()
{
    query = "SELECT * FROM LEVERAGE";


        try {
            prstatement = connection.prepareStatement(query);
            System.out.println("Got prstmt!");
        } catch (SQLException ex) {
            Logger.getLogger(myfxmlController.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs = prstatement.executeQuery();
            if(rs != null)
            {
            System.out.println("Got Result Set!");
            }
            else
            {
                System.out.println("No Result Set!");
            }
        } catch (SQLException ex) {
            Logger.getLogger(myfxmlController.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while(rs.next()) {
                String t = rs.getString(1);
                System.out.println("" + t);
                lst.add(t);
            }
            System.out.println("Got Result in List!");

        } catch (SQLException ex) {
            Logger.getLogger(myfxmlController.class.getName()).log(Level.SEVERE, null, ex);


        }


}

}

` 这是堆栈跟踪

 Exception in Application start method
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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/9727497.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at testfx.Testfx.start(Testfx.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/17342247.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/14208992.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/11822785.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/31501478.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/29531133.run(Unknown Source)
    ... 1 more
Exception running application testfx.Testfx
Java Result: 1

项目结构是

Source packages
-     testfx(Package)
-         Testfx.java
-         myfxml.fxml
-         myfxmlcontroller.java

libraries
-  derby.jar
-  derbyclient.jar
-  derbyrun.jar

大家好! 非常感谢帮助。我终于弄明白了。一行代码就创造了这一切。 行:

private ArrayList<String> lst = null;

应该是:

private ArrayList<String> lst = new ArrayList();

构造函数是必要的,问题解决了!谢谢你的帮助。 :)

【问题讨论】:

  • 这个错误意味着FXMLLoader找不到你的fxml文件。按照您的设置方式,myfxml.fxml 应该与Testfx 类在同一个包中(即它应该在testfx 中)。你能用你的项目结构更新问题吗?
  • 项目结构看起来正确。你能检查一下 fxml 文件是否被导出到构建目录(即它应该被复制到 .class 文件所在的位置)吗?
  • 你确定它正在加载 fxml 吗?如果你在控制器的initialize() 方法的第一行放了一个System.out.println(...);,你看到它的输出了吗?或者,如果您注释掉 poplist() 的正文,而只是将几个硬编码字符串添加到列表中,它会起作用吗?
  • 是的,它开始初始化方法。我将所有消息都发送到 GotResultset。接下来它应该是在不存在的列表中得到结果。与数据库的连接也很好。结果集确实具有所需的值。但不知何故,它没有将该字符串添加到 lst。执行停止并出现上述错误。

标签: java combobox javafx


【解决方案1】:

为什么要使用 prepareStatement?我不是很喜欢德比,但如果我在 SQLite 中这样做,它看起来像这样:

            try {
            Statement stmt;
            stmt = c.createStatement();
            stmt.execute("your statement");
            stmt.close();
            c.commit();
        }
        catch (SQLException f) {
            System.err.println(f.getClass().getName() + ": " + f.getMessage());
        }

您应该始终关闭您的语句此外,我认为您必须通过 c.commit() 提交您的查询。我会这样写你的方法:

public void poplist(){
query = "SELECT * FROM LEVERAGE";


    try {
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query); 
        while (rs.next()) {
            String t = rs.getString(1);
            System.out.println("" + t);
            lst.add(t);   
        } 
        statement.close();
        connection.commit();

    } catch (SQLException ex) {
        Logger.getLogger(myfxmlController.class.getName()).log(Level.SEVERE, null, ex);
    }


    }

}

希望这会有所帮助!

【讨论】:

  • 在代码到达此阶段之前发生错误。 PreparedStatements 通常更可取,尽管在这种情况下它不会立即产生任何影响。
  • 啊好吧。是的,异常表明加载程序找不到 fxml 文件,但是为什么当他删除 popmethod 时他的代码可以工作?引用他的话:(..好像我删除了poplist方法它工作正常。)如果它找不到fxml,在初始化时不应该弹出nullpointerexception吗?
  • @LOLWTFasdasdasdad 这就是我不明白的。当我们在 netbeans 中创建项目时,这基本上是默认程序。它可能不是 fxml 问题。我也试图给它路径,但没有相同的错误。正确获取数据库也没有问题。
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2021-01-13
  • 2021-03-09
  • 1970-01-01
  • 2018-05-07
相关资源
最近更新 更多