Results 1 to 4 of 4
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default 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
    Java 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

    Java 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

    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;
    		
    	}
    
    }
    the error:
    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)
    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! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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 ***:

    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;
             }
    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.

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException when accessing array

    Quote Originally Posted by aianta View Post
    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.

Similar Threads

  1. Accessing objects in array list
    By kev670 in forum New To Java
    Replies: 6
    Last Post: 03-11-2011, 01:49 AM
  2. Got problem on accessing public (global) array
    By smtwtfs in forum New To Java
    Replies: 6
    Last Post: 02-15-2011, 10:02 AM
  3. Accessing a file, assigning it to an array
    By BariMutation in forum New To Java
    Replies: 3
    Last Post: 11-10-2010, 09:20 PM
  4. Replies: 0
    Last Post: 05-23-2009, 05:26 PM
  5. Accessing array in other class
    By ce3c in forum New To Java
    Replies: 8
    Last Post: 02-23-2009, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •