简体   繁体   English

当我尝试运行javafx应用程序时在初始化中收到错误

[英]getting an error in the initialize when I try to run my javafx application

I was testing JavaFX application and I'm getting errors now don't know much about JavaFX , still learning it. 我正在测试JavaFX应用程序,但现在遇到错误,现在对JavaFX不太了解,仍然在学习它。 I think its because of the label.setText(numbers[0]); 我认为这是因为label.setText(numbers[0]); and if I do @FXML Label lebal = new Label(); 如果我这样做@FXML Label lebal = new Label(); , it wont change on action. ,它不会改变动作。 Thanks. 谢谢。

my gui class 我的gui课

package fx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class GUI extends Application {


@Override
public void start(Stage primaryStage) throws Exception {

    final Parent root = FXMLLoader.load(getClass().getResource("test.fxml")); //error (GUI.java:15)
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(new Scene(root, 257, 474));
    primaryStage.setResizable(false);
    primaryStage.show();
    ;

}

public static void main(String[] args) {
    GUI.launch(GUI.class, args);
}
}

my controller class: 我的控制器类:

package fx;

import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;

import com.sun.javafx.collections.ObservableListWrapper;

public class myCon implements Initializable {

@FXML Button button;
@FXML ComboBox<String> combobox;
@FXML Label label;


String[] order = {"First", "second"}; 
String[] numbers = {"one", "two"}; 


@Override
public void initialize(URL location, ResourceBundle resources) {
    combobox.setItems(new ObservableListWrapper<>(Arrays.asList(order)));
    combobox.getSelectionModel().selectFirst();
    label.setText(numbers[0]);//error here (myCon.java:32)
    System.out.println(label.getText());
    button.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
label.setText(numbers[combobox.getSelectionModel().getSelectedIndex()]);
            System.out.println(label.getText());
        }
    });
}

} }

the error I'm getting 我得到的错误

Jul 01, 2013 11:45:24 AM javafx.fxml.FXMLLoader logException
SEVERE: java.lang.NullPointerException
/C:/Users/user/Desktop/New%20folder%20(2)/Project/bin/fx/test.fxml:-1
at fx.myCon.initialize(myCon.java:32)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2047)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:1900)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2486)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2478)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2472)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2461)
at fx.GUI.start(GUI.java:15)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:315)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:174)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start          method
at      com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:399)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at fx.myCon.initialize(myCon.java:32)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2047)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:1900)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2486)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2478)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2472)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2461)
at fx.GUI.start(GUI.java:15)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:315)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:174)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
... 1 more

Your field 您的领域

Label label;

Is not initialized - that is, it is still null. 未初始化-也就是说,它仍然为null。

Your injection annotation @FXML (see tutorial) has obviously failed to load an instance. 您的注入批注@FXML (请参见教程)显然无法加载实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试运行我的游戏时,我一直得到(Suspended(NullPointerException)),但我无法在任何地方找到错误 - I keep getting (Suspended(NullPointerException)) when I try to run my game, but I cant find the error anywhere 当我尝试在 Java 中运行我的套接字程序时,为什么会出现错误? - Why am I getting an error when I try to run my socket program in Java? 尝试在 Netbeans 上运行 JavaFx 项目时,为什么我在 MacOS 上不断收到此 SIGBUS 错误代码? - Why do I keep getting this SIGBUS error code on my MacOS when trying to run a JavaFx Project on Netbeans? JavaFX 错误:当我尝试运行我的 Z6849095FCDBF43AD7Z(IntelliJ)文件时,无法找到或加载主 class home.main - JavaFX Error : Could not find or load main class home.main with gradle (IntelliJ) when I try to run my jar file 我尝试初始化类时的JavaFx调用目标异常 - JavaFx invocation target exception when I try to initialize class 当我尝试通过拖放运行我的Android应用程序时遇到意外错误 - Getting an unexpected error when I try to run my Android app with drag and drop 当我尝试运行我的Junits时获取NoTestsRemainException - Getting NoTestsRemainException when I try to run my junits 尝试运行此应用时收到setOnClickListener错误 - Getting a setOnClickListener error when I try to run this app 当我尝试运行我刚刚创建的 jar 文件时出现 Maven 错误“无法初始化主类” - Maven error “Unable to initialize main class” when I try to run a jar file I just created 尝试运行程序时出现Ant错误 - Ant error when I try to run my program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM