0

I'm getting a null point error, but I don't know why. Below are the classes which I use.

Kofferslot.java

package com.tvh;

/**
 * @author 
 */
public class Kofferslot {
    private static Letter[] letterlijst;
    private static Cijfer cijfer = new Cijfer();

    Kofferslot(){
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();

        Cijfer cijfer = new Cijfer();

    }

    Kofferslot(char firstLetter, char secondLetter, int cijfer){
        letterlijst[0].setLetter(firstLetter);
        letterlijst[1].setLetter(secondLetter);

        this.cijfer.setCijfer(cijfer);
    }

    public static String volgende(){

        letterlijst[0].volgende();
        char res1 = letterlijst[0].getLetter();

        letterlijst[1].volgende();
        char res2 = letterlijst[1].getLetter();

        cijfer.volgende();
        int rescijfer = cijfer.getCijfer();

        String result = String.valueOf(res1 + res2 + rescijfer);

        return result;

    }

    public static void main(String args[]){
        Kofferslot.volgende();
    }

}

Letter.java

package com.tvh;

/**
 * @author 
 */
public class Letter {
    private char letter;

    Letter() {
        char letter = 'A';
    }

    char getLetter() {
        return letter;
    }

    void setLetter(char letter) {
        this.letter = letter;

    }

    void volgende() {
        if (this.letter > 'Z') {
            letter = 'A';
        } else {
            letter++;
        }
    }

    public static void main(String[] args) {
        Letter letter = new Letter();
        letter.setLetter('A');
        letter.volgende();

        char letter1 = letter.getLetter();

        letter.setLetter('B');
        letter.volgende();

        char letter2 = letter.getLetter();

        System.out.print(letter1);
        System.out.print(letter2);
    }
}


package com.tvh;

/**
 * @author 
 */
public class Cijfer {
    private  static int cijfer;

    public Cijfer(){
        cijfer = 0;
    }

    public static int getCijfer(){
        return cijfer;

    }

    public void setCijfer(int cijfer){
        this.cijfer = cijfer;

    }

    public void volgende(){
        cijfer++;
    }

    public static void main(String[] args){
        System.out.println("Get cijfer object: " + Cijfer.getCijfer());

        Cijfer test = new Cijfer();
        System.out.println("New object: " + Cijfer.getCijfer());

    }


}

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
    at com.tvh.Kofferslot.volgende(Kofferslot.java:26)
    at com.tvh.Main.main(Main.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De`enter code here`legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

I hope that you guys can help me finding the nullpointexception!

Thanks in advance! Because for now I'm stuck..

TVH7
  • 305
  • 1
  • 4
  • 16
  • 1
    At least give a stack trace ! I think you need to initialize Letter[] letterlijst; before creating 2 objects in it. – StackFlowed Nov 16 '15 at 20:37
  • @StackFlowed Done. To others; I cannot seem to find the solution. Please do not mark this as dupe, since that didn't help me. I need a push in the right direction... – TVH7 Nov 16 '15 at 20:40
  • did you initialize Letter[] letterlijst; before creating 2 objects in it – StackFlowed Nov 16 '15 at 20:41
  • 1
    No check the duplicate question to understand what you are missing. (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – StackFlowed Nov 16 '15 at 20:44
  • @StackFlowed When I do String letterlijst; int cijfer;. It says that the var is already defined in the scope. – TVH7 Nov 16 '15 at 20:51
  • Please read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it & http://stackoverflow.com/q/3988788/2775450 it should answer all your questions. – StackFlowed Nov 16 '15 at 20:56
  • @StackFlowed I allready did. – TVH7 Nov 16 '15 at 21:00
  • I'm sorry please try to understand and not just blindly read it. – StackFlowed Nov 16 '15 at 21:01
  • NullPointerException is only the symptom here. Try to understand how `static` works, that is the root cause of your problem. – DJ. Nov 16 '15 at 21:10
  • The first thing that any program does is call the `main` function. Since it is static, it doesn't call the constructor of you class, meaning that letterlijst is never initialized. In your particular case you're probably getting confused because you have 3 main methods - maybe you're intending to call a different one? Otherwise you need in instantiate your class and remove the `static` keyword scattered throughout your code – Jeff Nov 16 '15 at 21:21

0 Answers0