0

So i'm building an application wich is asking the language to the user. But when i run it I get a NullpointerException and I can't seem to find the mistake i hope you guys can help me out.

this is my starter class

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);

 }

}

And this is the class where i validate the language

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");
    }
 }
}

and this is the error i get

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
  • Can you show us the exception stack trace – Youssef NAIT Mar 17 '16 at 13:34
  • 1
    `taal` is not initialized – Tommaso Bertoni Mar 17 '16 at 13:34
  • See also [how to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Andy Turner Mar 17 '16 at 13:35
  • 2
    `this.taal = taal` is a self-assignment. If it's already null, it stays null. You need to pass in a `Taal` instance as a parameter. – Andy Turner Mar 17 '16 at 13:36
  • 1
    `taal = new Taal(keuzeTaal);` should work then? – Desmedtj09 Mar 17 '16 at 13:55
  • `this.taal = taal;` in the `Start Method` is where your problem is. I don't want you to think it's in the `setTaal` method. Also don't compare strings like this `if(taal == "nl" || taal == "fr" || taal == "en")`. Instead it should be `"nl".equals(taal)`. To do it properly you want to check if `tall != null` and then do your equals. Even better it would be making a `private static final String` field for "nl", "fr" and "en" and then doing an `equals` to that. – wiredniko Mar 17 '16 at 13:56
  • thank you for the help. I solved it – Desmedtj09 Mar 17 '16 at 14:02

0 Answers0