Jan 10, 2017 07:09 AM
(This post was last modified: Dec 9, 2024 12:28 PM by Omniscient.)
Hi,
I'm creating a assignment for school. I'm getting this NullPointerException error and I don't know to solve it.
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:
And the main file, where I create the object:
I've tryed to initialise the array before the constructor, with;
And I tryed this:
But both solutions I tryed won't work. I hope someone can help me out in here.
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.



![[Image: 4jbrxO5.png]](https://i.imgur.com/4jbrxO5.png)