即使对象存在,使用javafx的NullpointerException?

时间:2017-03-26 21:55:35

标签: java user-interface javafx

maincontroller

package treetest;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;



/**
 *
 * @author akhil kumar
 */
public class maincontroller {

    @FXML
    private GraphicsContext gc;
    private Canvas canvas;
    private TreeView<String> bro;
    TreeItem<String> root=new TreeItem<>("root");
    TreeItem<String> child = new TreeItem<>("child");
    TreeItem<String> child2 = new TreeItem<>("child");
    TreeItem<String> child3 = new TreeItem<>("child");
    TreeItem<String> child4 = new TreeItem<>("child");
    TreeItem<String> child5 = new TreeItem<>("child");

    public void initialize(){

         root.getChildren().add(child);
         root.getChildren().add(child2);
         root.getChildren().add(child3);
         root.getChildren().add(child4);
         root.getChildren().add(child5);

        bro.setRoot(root);
        bro.setShowRoot(false);
       gc=canvas.getGraphicsContext2D();
       gc.setFill(Color.WHITE);
       gc.fillRect(0,0,canvas.getWidth(),canvas.getHeight());

    }


}

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.canvas.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="600.0" minWidth="800.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="treetest.maincontroller">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <HBox>
               <children>
                  <Button fx:id="catchment" mnemonicParsing="false" text="Button" />
                  <Button fx:id="output" mnemonicParsing="false" text="Button" />
                  <Button fx:id="junction" mnemonicParsing="false" text="Button" />
                  <Button fx:id="run" mnemonicParsing="false" text="Button" />
               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" top="10.0" />
               </padding>
            </HBox>
         </children>
      </VBox>
   </top>
   <center>
      <Canvas fx:id="canvas" height="533.0" width="592.0" BorderPane.alignment="CENTER">
         <cursor>
            <Cursor fx:constant="CROSSHAIR" />
         </cursor>
      </Canvas>
   </center>
   <left>
      <TreeView fx:id="bro" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
</BorderPane>

即使canvas和treeview的fx:id与emain控制器中的id匹配,我也会得到一个空指针异常

bro.setRoot(root)
bro.setShowRoot(false);
gc=canvas.getGraphicsContext2D();


basically in any line itry to use the canvas and treeview i am getting a nullpointer exception 


error log 

file:/C:/Users/akhil%20kumar/Documents/NetBeansProjects/treetest/dist/run720357897/treetest.jar!/treetest/tree.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
    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 treetest.Treetest.start(Treetest.java:30)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    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$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.loadImpl(FXMLLoader.java:2566)
    ... 17 more
Caused by: java.lang.NullPointerException
    at treetest.maincontroller.initialize(maincontroller.java:52)
    ... 27 more

我的代码出了什么问题?

第52行

bro.setRoot(root);

奇怪的部分是当我使用graphiccontext gc和初始化函数中的画布删除所有行时,它完全正常工作。

这也特别说明为什么控制器无法在fxml文件中找到对象,因此nullpointer异常的一般答案可能无法帮助我

无法理解为什么会这样?

0 个答案:

没有答案