label.setText处的Java NullPointerException

时间:2017-03-04 14:38:07

标签: java javafx controller fxml fxmlloader

当我运行我的应用程序时,它会打开,当我按下按钮以产生随机数时,我会得到一个NullPointerException。我是JavaFX的新手,所以我不知道如何解决这个问题。 主要课程:

package application;

import java.io.IOException;

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

public class BobMarley extends Application {

    FXMLLoader loader = new FXMLLoader();

    public BobMarley() throws IOException {

    }

    public void start(Stage stage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("application/BobMarleyLayout.fxml"));
        Scene scene = new Scene(root, 700, 300);

        stage.setTitle("Bob Marley");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

控制器:

import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class Controller implements Initializable {
    @FXML
    Label l;
    Label l1;
    Label l2;
    Label l3;
    Label l4;
    Label l5;
    Label l6;
    Random r = new Random();
    Random r1 = new Random();
    Random r2 = new Random();
    Random r3 = new Random();
    Random r4 = new Random();
    Random r5 = new Random();
    boolean b1;
    boolean b2;
    int z1;
    int z2;
    int z3;
    int z4;
    int z5;
    int z6;

    public Controller() {
        b1 = r.nextBoolean();
        Random r0 = new Random();
        b2 = r0.nextBoolean();
        z1 = r1.nextInt(15) + 5;
        z2 = r2.nextInt(15) + 5;
        z3 = r3.nextInt(15) + 5;
        z4 = r4.nextInt(15) + 5;
        z5 = r5.nextInt(15) + 5;
        z6 = z1 + z2 + z3;
    }

    @FXML
    private void handleButtonAction(ActionEvent event) {
        l.setText(Integer.toString(z1));
        l1.setText(String.valueOf(z2 + " " + "|"));
        l2.setText(String.valueOf(z3 + " " + "|"));
        l3.setText(String.valueOf(z4 + " " + "|"));
        l3.setVisible(b1);
        l4.setText(String.valueOf(z5 + " " + "|"));
        l4.setVisible(b2);
        l5.setText("=");
        if (b1 == true && b2 == true && z6 + z4 + z5 >= 65) {
            l6.setText("NOPE");
        } else if (b1 == true && b2 == true) {
            l6.setText(String.valueOf(z6 + z4 + z5));
        } else if (b1 == true && b2 == false && z6 + z4 + z5 >= 65) {
            l6.setText("NOPE");
        } else if (b1 == true && b2 == false) {
            l6.setText(String.valueOf(z6 + z4));
        } else if (b1 == false && b2 == true && z6 + z4 + z5 >= 65) {
            l6.setText("NOPE");
        } else if (b1 == false && b2 == true) {
            l6.setText(String.valueOf(z6 + z5));
        } else {
            l6.setText(String.valueOf(z6));
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

我的FXML文件:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>


<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" fx:controller="application.Controller"
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <MenuBar VBox.vgrow="NEVER">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="New" />
                        <MenuItem mnemonicParsing="false" text="Open…" />
                        <Menu mnemonicParsing="false" text="Open Recent" />
                        <SeparatorMenuItem mnemonicParsing="false" />
                        <MenuItem mnemonicParsing="false" text="Close" />
                        <MenuItem mnemonicParsing="false" text="Save" />
                        <MenuItem mnemonicParsing="false" text="Save As…" />
                        <MenuItem mnemonicParsing="false" text="Revert" />
                        <SeparatorMenuItem mnemonicParsing="false" />
                        <MenuItem mnemonicParsing="false" text="Preferences…" />
                        <SeparatorMenuItem mnemonicParsing="false" />
                        <MenuItem mnemonicParsing="false" text="Quit" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Undo" />
                        <MenuItem mnemonicParsing="false" text="Redo" />
                        <SeparatorMenuItem mnemonicParsing="false" />
                        <MenuItem mnemonicParsing="false" text="Cut" />
                        <MenuItem mnemonicParsing="false" text="Copy" />
                        <MenuItem mnemonicParsing="false" text="Paste" />
                        <MenuItem mnemonicParsing="false" text="Delete" />
                        <SeparatorMenuItem mnemonicParsing="false" />
                        <MenuItem mnemonicParsing="false" text="Select All" />
                        <MenuItem mnemonicParsing="false" text="Unselect All" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                    <items>
                        <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0"
            prefWidth="-1.0" VBox.vgrow="ALWAYS" id="root">
            <children>
                <Button layoutX="294.0" layoutY="219.0" mnemonicParsing="false"
                    onAction="#handleButtonAction" text="Let it Rip" />
                <Label id="l6" layoutX="499.0" layoutY="182.0" text="1" />
                <Label id="l" layoutX="184.0" layoutY="182.0" text="1"/>
                <Label id="l1" layoutX="245.0" layoutY="182.0" text="1"/>
                <Label id="l2" layoutX="256.0" layoutY="182.0" text="1"/>
                <Label id="l3" layoutX="318.0" layoutY="182.0" text="1"/>
                <Label id="l4" layoutX="385.0" layoutY="182.0" text="1"/>
                <Label layoutX="445.0" layoutY="182.0" text="=" />
                <Label layoutX="160.0" layoutY="26.0" text="Bob Marley"
                    textAlignment="CENTER">
                    <font>
                        <Font name="HelveticaNeueLTStd-Lt" size="74.0" />
                    </font>
                </Label>
            </children>
        </AnchorPane>
    </children>
</VBox>

我收到错误消息:

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:8413)
    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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NullPointerException
    at application.Controller.handleButtonAction(Controller.java:53)
    ... 58 more

感谢您的帮助! 路德维希

0 个答案:

没有答案