I am playing around with java, and have run into an issue when I utilize custom constructors.
When I begin my program, the main file contains a line like this:
Word input = new Word(word);
The constructor for word looks like this:
public Word(String s){
wordArray = s.toCharArray();
protocol = new Protocol(wordArray.length);
}
And the protocol looks like this:
public Protocol(int length)
{
letterList[0] = 'a';
letterList[1] = 'b';
letterList[2] = 'c';
letterList[3] = 'd';
letterList[4] = 'e';
letterList[5] = 'f';
letterList[6] = 'g';
letterList[7] = 'h';
letterList[8] = 'i';
letterList[9] = 'j';
letterList[10] = 'k';
letterList[11] = 'l';
letterList[12] = 'm';
letterList[13] = 'n';
letterList[14] = 'o';
letterList[15] = 'p';
letterList[16] = 'q';
letterList[17] = 'r';
letterList[18] = 's';
letterList[19] = 't';
letterList[20] = 'u';
letterList[21] = 'v';
letterList[22] = 'w';
letterList[23] = 'x';
letterList[24] = 'y';
letterList[25] = 'z';
wordLength = length;
for(int i=0;i<wordLength*2-1;i++)
{
display[i] = '_';
i++;
display[i] = ' ';
}
}
I am getting a NullPointerException at the line within my main file where word is constructed, and then protocol .
I have experimented with the code, and noticed if I do not call the constructor for the protocol within word the build is successful past that point, but I need protocol to be built later as well, so it still has issues.
Am I not allowed to call a constructor within another constructor? Anyone have any ideas on what may be going on?
If you need more clarification, please let me know!
No, you are definitely allowed to do that.
Clearly,
wordisnullwhen passed to theWordconstructor at this line: