1

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!

4
  • 3
    So what's the value of s you're passing? Commented Jan 28, 2013 at 5:03
  • You dont need an array of letters, you can just use a + index. Commented Jan 28, 2013 at 5:03
  • 1
    @Swapnil as if you need to ask. Clearly: null! Commented Jan 28, 2013 at 5:04
  • 1
    Please post the stack trace and indicate or post the exact line of code referred to at the top of the stack trace. Commented Jan 28, 2013 at 5:15

2 Answers 2

5

Am I not allowed to call a constructor within another constructor?

No, you are definitely allowed to do that.

Anyone have any ideas on what may be going on?

Clearly, word is null when passed to the Word constructor at this line:

Word input = new Word(word);
Sign up to request clarification or add additional context in comments.

4 Comments

I agree this is the simple answer. However, in lines above the word constructor, I call System.out.println(word), and it prints the word as expected. Thoughts?
Then as Abu suggested, you may have failed to initialize letterList.
Then step through your code with a debugger, and see what variable or field is null that your code is trying to dereference.
Ah! I found it! Good call on the failure to initialize. I have another array which I failed to initialize after the letterList. After I fixed the first I didn't notice the line number change until I went back a few times! Thanks so much!
3

The problem may be because you might not have initialized you letterList array, so try initializing it first before using it.

public Protocol(int length)
{
   letterList = new char[26];  
   letterList[0] = 'a';  
   letterList[1] = 'b';  
   letterList[2] = 'c';  
   ...  
   ...

}

1 Comment

Indeed, this is another possible issue. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.