大佬教程收集整理的这篇文章主要介绍了无法调用“Object.getClass()”,因为“obj”为空 - JavaFX,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在试验 JavaFX 中的一个程序,该程序并排显示三个图像以及它们下方的三个按钮。当您单击一个按钮时,按钮下方的卡片应该替换为另一个图像。我正在尝试为此使用单个事件处理程序,因此需要在 start() 方法之外声明我的 imageVIEw 对象和按钮。但是,我在程序中甚至不存在的一行代码上收到 NullPointerException。
这是我的代码。所有这些代码都在同一个 Main 类中。
package application;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import javafx.geometry.*;
import java.io.*;
public class Main extends Application {
//IMAGES
//img 1 setup
final String img1Path = "src/application/c1.gif";
ImageVIEw img1;
Boolean img1Flip = false;
//img 2 setup
final String img2Path = "src/application/c2.gif";
ImageVIEw img2;
Boolean img2Flip = false;
//img 3 setup
final String img3Path = "src/application/c3.gif";
ImageVIEw img3;
Boolean img3Flip = false;
//img 4 setup
final String img4Path = "src/application/b1fv.gif";
//buttonS
//button 1 setup
button button1;
//button 2 setup
button button2;
//button 3 setup
button button3;
@OverrIDe
public voID start(Stage stage1) throws fileNotFoundException {
System.out.println("Compiled."); //deBUG
//image vIEw setup
img1 = new ImageVIEw(new Image(new fileinputStream(img1Path))); //creates an image vIEw
img2 = new ImageVIEw(new Image(new fileinputStream(img2Path))); //creates an image vIEw
img3 = new ImageVIEw(new Image(new fileinputStream(img3Path))); //creates an image vIEw
//button setup
button1 = new button("Flip");
button1.setonAction((EventHandler<ActionEvent>) new buttonHandler());
//button1.setID("button1");
button2 = new button("Flip");
button2.setonAction((EventHandler<ActionEvent>) new buttonHandler());
//button2.setID("button2");
button3 = new button("Flip");
button3.setonAction((EventHandler<ActionEvent>) new buttonHandler());
//button3.setID("button3");
//scene setup
final int sceneW = 400;
final int sceneH = 200;
//stage setup
final String stagetitle = "FlipImages";
System.out.println("Initialized."); //deBUG
try {
//STAGE SETUP
System.out.println("SetTing Up"); //deBUG
stage1.settitle(stagetitlE);
FlowPane pane = new FlowPane(OrIEntation.VERTICAL);
Scene scene1 = new Scene(pane,sceneW,sceneH);
pane.setAlignment(Pos.CENTER);
//ADDING TO SCENE
System.out.println("FormatTing."); //deBUG
HBox imagePanel = new HBox(img1,img2,img3);
HBox buttonPanel = new HBox();
imagePanel.setSpacing((doublE) 5);
buttonPanel.setSpacing((doublE) 10);
pane.getChildren().addAll(imagePanel,buttonPanel);
//FINAL STEPS
System.out.println("GeneraTing."); //deBUG
stage1.setScene(scene1);
stage1.setResizable(false);
stage1.show();
}
catch(Exception E) {
System.out.println("Exception Caught."); //deBUG
e.printstacktrace();
}
}
class buttonHandler implements EventHandler<ActionEvent> {
public voID handle(ActionEvent a) {
try {
System.out.println("handle Image Creation"); //deBUG
//regular image setup
Image img1unflip = new Image(new fileinputStream(img1Path)); //creates an image
Image img2unflip = new Image(new fileinputStream(img2Path)); //creates an image
Image img3unflip = new Image(new fileinputStream(img3Path)); //creates an image
//flipped image setup
Image imgFlipped = new Image(new fileinputStream(img4Path)); //creates an image
System.out.println("Obtaining source"); //deBUG
//String callID = ((NodE) a.getsource()).getID(); //doesn't work well
Object source = a.getsource();
System.out.println("FlipPing"); //deBUG
if (source == button1) {
if (img1Flip == falsE) {
img1 = new ImageVIEw(imgFlipped);
}
else {
img1 = new ImageVIEw(img1unflip);
}
}
else if (source == button2) {
if (img2Flip == falsE) {
img2 = new ImageVIEw(imgFlipped);
}
else {
img2 = new ImageVIEw(img2unflip);
}
}
else if (source == button3) {
if (img3Flip == falsE) {
img3 = new ImageVIEw(imgFlipped);
}
else {
img3 = new ImageVIEw(img3unflip);
}
}
}
catch (Exception E) {
System.out.println("Exception Caught: handle"); //deBUG
e.printstacktrace();
}
}
}
public voID main(String[] args) {
launch(args);
}
}
这是控制台输出。请注意,我没有打印任何调试语句。
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegaTingMethodAccessorImpl.invoke(DelegaTingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.NullPointerException: CAnnot invoke "Object.getClass()" because "obj" is null
at java.base/java.lang.reflect.Method.invoke(Method.java:557)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
... 5 more
我想知道为什么会发生此错误以及如何修复它。
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的无法调用“Object.getClass()”,因为“obj”为空 - JavaFX全部内容,希望文章能够帮你解决无法调用“Object.getClass()”,因为“obj”为空 - JavaFX所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。