NullPointerException when accessing array
I am trying to add a word object to an array. Currently I am doing this by creating an array then finding if there is an empty (null) space in it, then selecting that part of the array and assigning the word object to it.
here is the code:
Execute.java the main execution class
Code:
public class Execute {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String sentence [] = {"The","cat","ran"};
WordManager test = new WordManager (3);
for (int i = 0; i< sentence.length; i++){
Word word = new Word(sentence[i],test);
}
for (int i = 0; i < test.getWords().length; i++){
System.out.println(test.getWords()[i]);
}
}
}
Word.java the class for the word object
Code:
import java.awt.Color;
import javax.swing.JLabel;
public class Word {
private JLabel wordLabel;
private Color defaultColor = Color.BLACK;
private String Name;
public Word (String text, WordManager manager) {
Name = text;
wordLabel = new JLabel(text);
wordLabel.setForeground(defaultColor);
manager.addWord(this);
}
public Word ChangeColor (Word word, Color color){
word.getLabel().setForeground(color);
return word;
}
public JLabel getLabel (){
return wordLabel;
}
public String getName(){
return Name;
}
}
WordManager.java the class that keeps track of the words in a sentence
Code:
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class WordManager {
private Word Words [];
public WordManager (int arrayLength){
Word Words[] = new Word [arrayLength];
}
public void addWord (Word wordAdd){
//Check if array is already full
//Initialize counter to count number of words in array
int counter = 0;
//Count number of words or if empty space in array break loop
for (int i = 0; i < Words.length; i++){
if (Words[i] == null){
break;
}
counter++;
}
// If the number of words in array are the same as the array length report error then exit
if (counter == Words.length){
JOptionPane.showMessageDialog(null, "Can not add any new words. Array is full!");
System.exit(0);
}
// Find empty space in array and add word
for (int i = 0; i < Words.length; i++){
if(Words[i] == null){
Words[i] = wordAdd;
}
}
}
public Word [] getWords(){
return Words;
}
}
the error:
Code:
Exception in thread "main" java.lang.NullPointerException
at AlgorithmAnalyzer.WordManager.addWord(WordManager.java:24)
at AlgorithmAnalyzer.Word.<init>(Word.java:19)
at AlgorithmAnalyzer.Execute.main(Execute.java:17)
Maybe, I am approaching this in a wrong way. How can I add words to my word manager so that I can refer back to them later?
I would prefer to use arrays rather than arrayLists if at all possible. I dont know if it is possible though so tell me if using an arrayList is a must.
Thanks in advance! :)
Re: NullPointerException when accessing array
A rule of thumb to try to follow: when you have a NullPointerException, always carefully check the line that throws the exception. Usually knowledge of which line causes the exception will clue you in on which variable is null, and then it's often just a matter of tracking back through your code to see why, as is the case here. The line causing the exception is marked below with the comment // *** NPE ***:
Code:
class WordManager {
private Word Words[]; // *** A ***
public WordManager(int arrayLength) {
Word Words[] = new Word[arrayLength]; // *** B ***
}
public void addWord(Word wordAdd) {
int counter = 0;
for (int i = 0; i < Words.length; i++) { // *** NPE ***
if (Words[i] == null) {
break;
}
And there's only one reference variable in that line that can possibly cause this exception, Words. If you track back in this code, you'll see that the Words is declared on the line marked // *** A *** and it is supposedly constructed on the line marked // *** B ***, but is it? If you look closely at the // *** B *** line, you'll see that you redeclare the variable there, and so you're only constructing a local variable, not the class one which is still null, and that's the problem and the solution -- don't redeclare the variable, just construct it.
Re: NullPointerException when accessing array
Thanks
Now that I look at this from that point of view I feel stupid. Ill try to review my code more carefully next time.
Re: NullPointerException when accessing array
Quote:
Originally Posted by
aianta
Thanks
Now that I look at this from that point of view I feel stupid. Ill try to review my code more carefully next time.
Not stupid, but you just haven't run into that error before. Now that you have, you will be able to help others when they bump into the same problem.