Занятие, чтобы узнать больше о Java и JavaFX. Не совсем уверен, что я делаю здесь не так. Я получаю исключение нулевого указателя при попытке запустить приложение.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CreditCardInput extends Application {
TextField creditCardNo;
TextField expDate;
Image creditCardImage;
TextField ccvInput;
String imageOutput;
ImageView imageView;
public CreditCardInput() {
this.creditCardNo = new TextField("xxxxxxxxxxxxxxxx");
this.expDate = new TextField("MM/YYYY");
this.ccvInput = new TextField("xxx");
}
public StackPane drawForm(){
StackPane layout = new StackPane();
for(int col=0;col<2;col++){
for(int row=0; row<4;row++){
}
}
return layout;
}
public String creditCardNumberValidate(){
Scanner ccnInput = new Scanner(System.in);
String ccInput = this.ccvInput.getText();
char[] intArray = ccInput.toCharArray();
StringBuilder doubleDigitCheck = new StringBuilder(intArray[0]+intArray[1]);
System.out.println(ccInput);
String str = new String(doubleDigitCheck);
boolean bMatches = str.matches("^[34]$");
int checkValue = Integer.parseInt(String.valueOf(intArray[0]));
String ccvOutput;
switch(checkValue){
case 3:
if(str.matches("^{34}$")){
return "amex.png";
}
if(str.matches("^[37]$")){
return "amex.png";
}
if(str.matches("^[35]$")){
return "jcb.png";
}
case 4:
ccvOutput = "visa.png";
case 5:
ccvOutput = "mastercard.png";
default:
ccvOutput = "invalid";
}
this.imageOutput = ccvOutput;
return this.imageOutput;
}
public ImageView cerditCardImage() {
try {
this.creditCardImage = new Image(new FileInputStream(imageOutput));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.imageView = new ImageView(this.creditCardImage);
return this.imageView;
}
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Credit Card Number");
Group root = new Group(this.imageView, drawForm());
FlowPane layout = new FlowPane();
Scene scene = new Scene(root,300,300);
Label cardNoTitle = new Label("Card Number");
Label expDateLabel = new Label("Exp Date");
Label cvvLabel = new Label("CVV");
layout.getChildren();
layout.getChildren().addAll(creditCardNo,expDateLabel,expDate,cvvLabel,ccvInput);
root.getChildren().add(layout);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
System.out.println("Launched!");
}
}
Сообщение об ошибке, которое я получаю:
/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/bin/java" "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=61275:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Volumes/external disk/dropbox/Dropbox/UCLA_School/Java2/CreditCardInput/out/production/CreditCardInput" CreditCardInput
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = Group@415d8a95
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:546)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at javafx.graphics/javafx.scene.Group.<init>(Group.java:106)
at CreditCardInput.start(CreditCardInput.java:96)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application CreditCardInput
Process finished with exit code 1
Любая помощь с благодарностью.