Results 1 to 4 of 4
- 10-15-2011, 08:05 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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
Word.java the class for the word objectJava 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]); } } }
WordManager.java the class that keeps track of the words in a sentenceJava 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; } }
the error:Java 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; } }
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?Java 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)
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 ***:
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.Java 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; }
- 10-15-2011, 10:17 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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.
-
Similar Threads
-
Accessing objects in array list
By kev670 in forum New To JavaReplies: 6Last Post: 03-11-2011, 12:49 AM -
Got problem on accessing public (global) array
By smtwtfs in forum New To JavaReplies: 6Last Post: 02-15-2011, 09:02 AM -
Accessing a file, assigning it to an array
By BariMutation in forum New To JavaReplies: 3Last Post: 11-10-2010, 08:20 PM -
[newbie] NullPointerException when accessing ActionEvent handler
By jon80 in forum New To JavaReplies: 0Last Post: 05-23-2009, 04:26 PM -
Accessing array in other class
By ce3c in forum New To JavaReplies: 8Last Post: 02-22-2009, 11:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks