【问题标题】:NullPointerException in changing javaFX scene更改 javaFX 场景中的 NullPointerException
【发布时间】:2014-09-30 03:21:20
【问题描述】:

我的 javaFx 项目中有两个场景 .. 第一个 Language.fxml 有一个按钮,单击该按钮可将场景更改为 allDevices.fxml .. 但它会抛出 NullPointerException 说“需要位置”,尽管这两个fxml 文件在同一路径中!!

这是我的LanguageController.java

package astrolabe;

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

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
 *
 * @author Ahmed Fawzy
 */
public class LanguageController implements Initializable {

    @FXML
    private Button arabic ;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        arabic.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

                try{
                    Node node=(Node) event.getSource();
                    Stage stage=(Stage) node.getScene().getWindow();
                    Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("allDevices.fxml"));/* Exception */
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }

            }
        });


    }

【问题讨论】:

  • System.out.println(getClass().getResource("allDevices.fxml").toExternalForm());记录你得到的URL,看看它是否给了你所期望的。
  • @James_D 添加System.out.println(getClass().getResource("allDevices.fxml").toExternalForm());‌​ 时出现语法错误!!
  • 输入:似乎复制和粘贴以某种方式包含一些使解析器感到困惑的不可见字符。
  • @James_D file:/D:/museum/Astrolabe/build/classes/astrolabe/allDevices.fxml 符合预期

标签: javafx fxml scene scenebuilder


【解决方案1】:

在fxml文件名前加上包名解决的问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多