Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Null Pointer Excpetion help......

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Null Pointer Excpetion help......

    Help needed .. Null pointer Exception..


     
    [B][I][U]//Execution class .......
    [/U][/I][/B]
     
    public class Execution {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		WordList wordList = new WordList();
    		Word word1 = new Word("abd"); 
    		Word word2 = new Word("abdede");
    		Word word3 = new Word("abddwd");                   // TODO Auto-generated method stub
    		wordList.addWord(word1);				//NUll Point Exception here.. Can some one explains why???
    		wordList.addWord(word2);
    		wordList.addWord(word3);
    		wordList.displayWordsOfLength(2);
     
    	}
     
    }
     
     
     
     
    [B][I][U]//////WordList Class[/U][/I][/B]
     
     
    import java.util.ArrayList;
     
    public class WordList {
     
    						ArrayList<Word> wordList;
     
     
    						public WordList() {
    							super();
     
    						}
     
    						public ArrayList<Word> getWordList() {
    							return wordList;
    						}
     
    						public void setWordList(ArrayList<Word> wordList) {
    							this.wordList = wordList;
    						}
    					public void addWord(Word word)                                 // NULL POINTER EXCEPTION PLEASE EXPLAIN WHY?
    					{
    						wordList.add(word);
    					}
    					public void displayWordsOfLength(int length)
    					{
    						for(Word word :wordList)
    						{
    							if(word.getWord().length()== length)
    								System.out.println("Word with length: "+length+"    "+word.getWord());
    					}
    					}
    						public void removeWord(String inputWord){
    							for(Word word :wordList)
    							{
    								if(word.getWord().equalsIgnoreCase(inputWord))
    								{
    									wordList.remove(word);
    									System.out.println("Word  "+inputWord+"  is removed  "+word.getWord());
     
    						}
    							}
     
    [I][U][B]//WORD CLASS[/B][/U][/I]
     
     
     
     
    public class Word {
     
    			private String word ;
    			private int length ;
     
    			public Word(String word) {
    				super();
    				this.word = word;
    			}
     
    			public String getWord() {
    				return word;
    			}
    			public void setWord(String word) {
    				this.word = word;
    			}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Null Pointer Excpetion help......

    Null pointer Exception..
    please post the full text of the error message. It shows where the error happened.
    Look at the line where the error happened, find the variable with the null value and backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Excpetion help......

    Exception in thread "main" java.lang.NullPointerException
    at WordList.addWord(WordList.java:24)
    at Execution.main(Execution.java:13)

    --- Update ---

    Exception in thread "main" java.lang.NullPointerException
    at WordList.addWord(WordList.java:24)
    at Execution.main(Execution.java:13)

    I back tracked but unable to get the key point.... I have added an object of Word class to Arraylist.
    Last edited by nutan; April 3rd, 2013 at 02:11 PM. Reason: Company policy reasons

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Null Pointer Excpetion help......

    Exception in thread "main" java.lang.NullPointerException
    at WordList.addWord(WordList.java:24)
    There is a variable with a null value on line 24. Look at line 24 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 24 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    nutan (April 3rd, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Excpetion help......

    Thanks !!!! i got it... i hadn't created the object of the ArrayList in the WordList class......
    public void addWord(Word word)
    					{
    						wordList = new ArrayList<Word>();      //this was missing in WordList.java
    						wordList.add(word);
    					}

Similar Threads

  1. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 11:21 AM
  2. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 08:32 PM
  3. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 11:07 AM
  4. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 10:25 PM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 01:48 AM