所以我正在构建一个向用户询问语言的应用程序。但当我运行它时,我得到一个NullpointerException,我似乎无法找到错误,我希望你们可以帮助我。
这是我的初级班
package ui;
import domein.*;
import java.util.Scanner;
public class Start {
private DomeinController domeincontroller;
private Taal taal;
private Speler speler;
public Start(DomeinController controller) {
this.domeincontroller = controller;
this.taal = taal;
String naam, kleur, keuzeTaal;
int geboortejaar, spelerAantal;
Scanner scan = new Scanner(System.in);
System.out.println("Geef de gewenste taal in (nl, fr, en): ");
keuzeTaal = scan.next();
taal.controlleerTaal(keuzeTaal);
}
}
这是我验证语言的类
package domein;
public class Taal {
private String taal;
public String getTaal(){
return taal;
}
public void setTaal(){
this.taal = taal;
}
public void controlleerTaal(String taal){
if(taal == "nl" || taal == "fr" || taal == "en"){
taal = taal;
}else{
throw new IllegalArgumentException("Gelieve een juiste taal te kiezen");
}
}
}
这是我得到的错误
Exception in Application start method
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:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at ui.Start.<init>(Start.java:32)
at main.StartUp.start(StartUp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
... 1 more
Exception running application main.StartUp
Java Result: 1