应用程序启动方法中的Javafx异常无法启动程序

时间:2015-01-11 18:26:26

标签: java combobox javafx

我正在尝试从数据库中获取一列并将其提供给组合框。它不起作用。我看到了类似错误的其他答案,但似乎不是一个案例,好像我删除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();

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

1 个答案:

答案 0 :(得分:0)

为什么使用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);
    }


    }

}

希望这有帮助!