为什么按钮为空?
Why is the Button Null?
我收到 NullPointerException。它说该按钮为空,这没有任何意义。下面列出了完整的错误代码和我的代码。
package com.example.effectslikerotation;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application implements EventHandler<ActionEvent> {
private Button blurButton, rotateButton, scaleButton;
private Rotate rotate;
private double angle;
Object buttonClicked;
Object buttonText;
@Override
public void start(Stage stage) throws IOException {
Button rotateButton = new Button();
Button blurButton = new Button();
Button scaleButton = new Button();
angle = 0.0;
rotateButton.setOnAction(this);
blurButton.setOnAction(this);
scaleButton.setOnAction(this);
rotateButton.setText("Rotate");
blurButton.setText("Blur");
scaleButton.setText("Scale");
Label reflect = new Label("I'm a reflection of you.");
FlowPane FProot = new FlowPane();
Scene scene = new Scene(FProot, 600, 500);
FProot.setAlignment(Pos.CENTER);
FProot.getChildren().addAll(rotateButton, blurButton, scaleButton, reflect);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
//rotateButton.getTransforms().add(rotate);
}
public static void main(String[] args) {
launch();
}
@Override
public void handle(ActionEvent e) {
Object buttonClicked = e.getSource();
if (rotateButton.isPressed()) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
if (buttonClicked.equals(blurButton)) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
if (buttonClicked.equals(scaleButton)) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
}}
应用程序线程”java.lang.NullPointerException:无法调用“javafx.scene.control.Button.isPressed()”,因为“this.rotateButton”为空
在 com.example.effectslikerotation/com.example.effectslikerotation.HelloApplication.handle(HelloApplication.java:73)
在 com.example.effectslikerotation/com.example.effectslikerotation.HelloApplication.handle(HelloApplication.java:20)
在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
在 javafx.base/javafx.event.Event.fireEvent(Event.java:198)
在 javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
在 javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
在 javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
在 javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
在 javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
在 javafx.base/javafx.event.Event.fireEvent(Event.java:198)
在 javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
在 javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
在 javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
在 java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
在 javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
在 javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
在 javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法)
在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
在 java.base/java.lang.Thread.run(Thread.java:833)
您定义这些字段:
private Button blurButton, rotateButton, scaleButton;
但永远不要给他们分配任何东西。
Button rotateButton = new Button();
Button blurButton = new Button();
Button scaleButton = new Button();
在这些赋值前添加 Button
表示您正在创建新变量。
要解决此问题,只需删除 Button
:
rotateButton = new Button();
blurButton = new Button();
scaleButton = new Button();
我收到 NullPointerException。它说该按钮为空,这没有任何意义。下面列出了完整的错误代码和我的代码。
package com.example.effectslikerotation;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application implements EventHandler<ActionEvent> {
private Button blurButton, rotateButton, scaleButton;
private Rotate rotate;
private double angle;
Object buttonClicked;
Object buttonText;
@Override
public void start(Stage stage) throws IOException {
Button rotateButton = new Button();
Button blurButton = new Button();
Button scaleButton = new Button();
angle = 0.0;
rotateButton.setOnAction(this);
blurButton.setOnAction(this);
scaleButton.setOnAction(this);
rotateButton.setText("Rotate");
blurButton.setText("Blur");
scaleButton.setText("Scale");
Label reflect = new Label("I'm a reflection of you.");
FlowPane FProot = new FlowPane();
Scene scene = new Scene(FProot, 600, 500);
FProot.setAlignment(Pos.CENTER);
FProot.getChildren().addAll(rotateButton, blurButton, scaleButton, reflect);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
//rotateButton.getTransforms().add(rotate);
}
public static void main(String[] args) {
launch();
}
@Override
public void handle(ActionEvent e) {
Object buttonClicked = e.getSource();
if (rotateButton.isPressed()) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
if (buttonClicked.equals(blurButton)) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
if (buttonClicked.equals(scaleButton)) { //
System.out.println("Rotate button clicked"); //not sure why this doesnt print out.
}
}}
应用程序线程”java.lang.NullPointerException:无法调用“javafx.scene.control.Button.isPressed()”,因为“this.rotateButton”为空 在 com.example.effectslikerotation/com.example.effectslikerotation.HelloApplication.handle(HelloApplication.java:73) 在 com.example.effectslikerotation/com.example.effectslikerotation.HelloApplication.handle(HelloApplication.java:20) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在 javafx.base/javafx.event.Event.fireEvent(Event.java:198) 在 javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792) 在 javafx.controls/javafx.scene.control.Button.fire(Button.java:203) 在 javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208) 在 javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在 javafx.base/javafx.event.Event.fireEvent(Event.java:198) 在 javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897) 在 javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878) 在 javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301) 在 java.base/java.security.AccessController.doPrivileged(AccessController.java:399) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450) 在 javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449) 在 javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557) 在 javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) 在 java.base/java.lang.Thread.run(Thread.java:833)
您定义这些字段:
private Button blurButton, rotateButton, scaleButton;
但永远不要给他们分配任何东西。
Button rotateButton = new Button();
Button blurButton = new Button();
Button scaleButton = new Button();
在这些赋值前添加 Button
表示您正在创建新变量。
要解决此问题,只需删除 Button
:
rotateButton = new Button();
blurButton = new Button();
scaleButton = new Button();