Hack Forums
[Java] NullPointerException - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] NullPointerException (/showthread.php?tid=5519589)



NullPointerException - Mohinder - Jan 10, 2017

Hi,

I'm creating a assignment for school. I'm getting this NullPointerException error and I don't know to solve it.

Code
Exception in thread "main" java.lang.NullPointerException
    at kofferslot.Kofferslot.<init>(Kofferslot.java:18)
    at kofferslot.Applicatie.main(Applicatie.java:7)
/Users/username/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

The exact error I get.
So the assignment is basically a password creator for a suitcase. The password will be something like AA0 or DZ1.

This is the complete code of the Kofferslot.java file:
Code
package kofferslot;

public class Kofferslot {
    
    private Letter[] letterlijst;
    private Cijfer cijfer;
    
    public Kofferslot() {
        
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();
        cijfer = new Cijfer();
        
    }
    
    public Kofferslot(char firstLetter, char secondLetter, int cijfer) {
        
        letterlijst[0].setLetter(firstLetter);
        letterlijst[1].setLetter(secondLetter);
        this.cijfer.setCijfer(cijfer);
                
    }
    
    public void volgende() {
        if(cijfer.getCijfer() != 9) {
            cijfer.volgende();
        } else if(cijfer.getCijfer() == 9) {
            cijfer.volgende();
            letterlijst[0].volgende();
            letterlijst[1].volgende();
        }
    }
    
}

And the main file, where I create the object:

Code
package kofferslot;

public class Applicatie {

    public static void main(String[] args) {
        
        Kofferslot A = new Kofferslot('A', 'A', 0);
        Kofferslot B = new Kofferslot('B', 'R', 9);
        Kofferslot C = new Kofferslot('D', 'Z', 9);
        Kofferslot D = new Kofferslot('Z', 'Z', 9);
        
        A.volgende();
        B.volgende();
        C.volgende();
        D.volgende();
        
    }
    
}

I've tryed to initialise the array before the constructor, with;

Code
package kofferslot;

public class Kofferslot {
    
    private Letter[] letterlijst = new Letter[2];
    private Cijfer cijfer;

And I tryed this:

Code
public class Kofferslot {
    
    private Letter[] letterlijst;
    private Cijfer cijfer;
    
    public Kofferslot() {
        
        letterlijst = new Letter[2];
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();
        cijfer = new Cijfer();
        
    }

But both solutions I tryed won't work. I hope someone can help me out in here.


* - Khamul - Jan 10, 2017

Where is 'Letter' defined? Shouldn't you be using 'char[]' instead of 'Letter[]'? Unless 'Letter' is some wrapper you've made for a char?

Try something like this:
Code
private Letter[] letterlijst;
/* blahblah */

public Kofferslot(char firstLetter, char secondLetter, int cijfer) {

    letterlijst = new Letter[] { new Letter(firstLetter), new Letter(secondLetter) };

}



* - Mohinder - Jan 10, 2017

(Jan 10, 2017 07:54 AM)ɴᴀᴢɢᴜʟ Wrote:  Where is 'Letter' defined? Shouldn't you be using 'char[]' instead of 'Letter[]'? Unless 'Letter' is some wrapper you've made for a char?

Try something like this:
Code
private Letter[] letterlijst;
/* blahblah */

public Kofferslot(char firstLetter, char secondLetter, int cijfer) {

    letterlijst = new Letter[] { new Letter(firstLetter), new Letter(secondLetter) };

}

I will show you the Letter.java file:

Code
package kofferslot;

public class Letter {

    private char letter;
    
    public Letter() {
        letter = 'A';
    }
    
    public char getLetter() {
        return letter;
    }
    
    public void setLetter(char letter) {
        this.letter = letter;
    }
    
    public void volgende() {
        letter++;
    }
    
}



* - Maxie - Jan 10, 2017

It seems like you are missing a proper constructor for the kofferslot, I might however be misstaken.


* - 3cli‌‌pse - Jan 10, 2017

You never actually allocate the array before setting anything in it, so you might want to do that

What you have:
Code
public Kofferslot() {
        
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();

What you should have:

Code
public Kofferslot() {
        letterlijst = new Letter[2]; // I assume 2, change it if need be
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();
}



* - Khamul - Jan 10, 2017

(Jan 10, 2017 10:47 AM)Wiz0rd Wrote:  You never actually allocate the array before setting anything in it, so you might want to do that

What you have:
Code
public Kofferslot() {
        
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();

What you should have:

Code
public Kofferslot() {
        letterlijst = new Letter[2]; // I assume 2, change it if need be
        letterlijst[0] = new Letter();
        letterlijst[1] = new Letter();
}

You might want to read the OP


* - Maxie - Jan 10, 2017

Hey! Looking at these two ^ posts over, I am missing the initation of the array. Put "()" after the [2].


* - 3cli‌‌pse - Jan 10, 2017

(Jan 10, 2017 11:02 AM)ɴᴀᴢɢᴜʟ Wrote:  You might want to read the OP

lmao. That's literally the only thing that could be null on that line, so no matter what he needs to do it.

(Jan 10, 2017 11:05 AM)Maxie Wrote:  Hey! Looking at these two ^ posts over, I am missing the initation of the array. Put "()" after the [2].

No.