在Controller JavaFX的initialize方法中获取NullPointerException

时间:2016-07-03 17:09:38

标签: java javafx nullpointerexception initialization stage

以下是我在控制器类的初始化方法中获得的NullPointerException:

`Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/W7/Documents/NetBeansProjects/PivotNSE/dist/run2050028213/PivotNSE.jar!/pivotnse/popupFXML.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173)
    ... 58 more
Caused by: java.lang.NullPointerException
    at pivotnse.PivotFXController.initialize(PivotFXController.java:294)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 66 more`

我要做的是,当点击myLoadButton时,会弹出一个新窗口 并在新窗口的标签中显示当前获取的所有符号 “下载”! (有点像用户的“实时更新”)。只是让用户知道,发生了什么。 以下是守则:

 public class PivotFXController implements Initializable {

        final ObservableList<GetPivots> data = FXCollections.observableArrayList();

        @FXML
        private TableView<GetPivots> pivotTable;

        @FXML
        private TableColumn<GetPivots, String> symbolColumn;
        @FXML
        private TableColumn<GetPivots, String> pivotColumn;
        @FXML
        private TableColumn<GetPivots, String> r1Column;
        @FXML
        private TableColumn<GetPivots, String> r2Column;

        @FXML
        private TableColumn<GetPivots, String> r3Column;

        @FXML
        private TableColumn<GetPivots, String> s1Column;
        @FXML
        private TableColumn<GetPivots, String> s2Column;
        @FXML
        private TableColumn<GetPivots, String> s3Column;
        @FXML
        private Label statusLabel;
        @FXML
        private Button myLoadButton;
        @FXML
        private Button doneButton;

        @FXML
        private TextField symbolField;

        private String SYMBOLS_FILE = "SYMBOLS.txt";

        private static List<String> symbolList = new ArrayList<String>();


        private boolean handleSymbol(String symbol) {
            System.out.println(symbol + " is the input");
            // statusLabel.setText("Downloading... " + symbol);
            FetchDataNSE fdn = new FetchDataNSE();

            if (fdn.getDataNSE(symbol).size() > 0) {
                CalculatePivot cp = new CalculatePivot(fdn.getDataNSE(symbol));
                GetPivots gp = new GetPivots(cp);
                data.add(gp);
                //   statusLabel.setText(symbol + " " + "Downloaded!");
                return true;
            }
            return false;
        }



        @FXML
        private void handleLoadButton(ActionEvent event) throws IOException {
            Stage stage;
            Parent root;
            if (event.getSource() == myLoadButton) {
                //get reference to the button's stage         
                stage = (Stage) myLoadButton.getScene().getWindow();
                //load up OTHER FXML document
                root = FXMLLoader.load(getClass().getResource("popupFXML.fxml"));// <-- ****this line is causing problem**** I am getting NULLPOINTEREXCEPTION from 1st line in `initialize` method WHY?
                stage.setScene(new Scene(root));
                stage.setTitle("DOWNLOADING STATUS");
                stage.initModality(Modality.APPLICATION_MODAL);
                stage.showAndWait();
                loadSymbols();

            } else {
                stage = (Stage) doneButton.getScene().getWindow();
                root = FXMLLoader.load(getClass().getResource("pivotFXGUI.fxml"));
                Scene scene = new Scene(root);
                stage.setScene(scene);
                stage.show();
            }


        }

        private void loadSymbols() {
            data.clear();
            symbolList.clear();

            BufferedReader br = null;

            try {

                String sCurrentLine;

                br = new BufferedReader(new FileReader(SYMBOLS_FILE));

                while ((sCurrentLine = br.readLine()) != null) {
                    symbolList.add(sCurrentLine.trim());
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            for (String sym : symbolList) {
                statusLabel.setText("Downloading... " + sym);
                handleSymbol(sym);
            }

            statusLabel.setText("LOADING SYMBOLS DONE!");
        }




        @Override
        public void initialize(URL url, ResourceBundle rb) {
    // 1st line below gives NullPointerException! why does that happen?
            symbolColumn.setCellValueFactory(new PropertyValueFactory<>("symbolProperty"));
            pivotColumn.setCellValueFactory(new PropertyValueFactory<>("pivotProperty"));
            r1Column.setCellValueFactory(new PropertyValueFactory<>("r1Property"));
            r2Column.setCellValueFactory(new PropertyValueFactory<>("r2Property"));
            r3Column.setCellValueFactory(new PropertyValueFactory<>("r3Property"));
            s1Column.setCellValueFactory(new PropertyValueFactory<>("s1Property"));
            s2Column.setCellValueFactory(new PropertyValueFactory<>("s2Property"));
            s3Column.setCellValueFactory(new PropertyValueFactory<>("s3Property"));
            pivotTable.setItems(data);

            // myLoadButton.fire();
            //  pivotTable.getColumns().addAll(symbolColumn,s3Column, s2Column,s1Column, pivotColumn, r1Column, r2Column, r3Column  );

        }


    }
`

为什么“initialize”方法中的第一行会抛出NullPointerException!? 我在handleLoadButton(..)方法和initialize(..)方法中做什么错误? 当我从initialize方法程序中删除所有内容时,单击myLoadButton时会打开一个新窗口,但是当调用loadSymbols()方法时会发生NullPointerException ...

ALSO ::: at pivotnse.PivotFXController.handleLoadButton(PivotFXController.java:173) ... 58 more Caused by: java.lang.NullPointerException at pivotnse.PivotFXController.initialize(PivotFXController.java:294) 第173行正在关注::: root = FXMLLoader.load(getClass().getResource("popupFXML.fxml")); 第294行是initialize()方法中的第1行。

2 个答案:

答案 0 :(得分:1)

fxml文档与控制器之间的关系按设计为1:1。换句话说,每个fxml文档都应该有自己的控制器。

目前您正在尝试使用popupFXML.fxml加载pivotnse.PivotFXController作为控制器。因此initialize()执行symbolColumn.setCellValueFactory()symbolColumn没有注入,因为popupFXML.fxml没有symbolColumn。结果:NPE。

你应该为popupFXML.fxml创建一个单独的控制器类,并在你的fxml中引用它,如fx:controller="pivotnse.PopupFXMLController"

答案 1 :(得分:0)

可能“symbolColumn”尚未初始化。也许你已经忘记了fx:文件中的fx:id。或者错误拼写了fx:id。