【发布时间】: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。执行停止并出现上述错误。