Results 1 to 13 of 13
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Why I'm getting "java.lang.NullPointerException"

    Hallo, i don not know why receiving ""java.lang.NullPointerException"" when instantiating Group[] group = new group[3];
    please check the 3 lines with comments


    Java Code:
    public static void main (String[] args) throws FileNotFoundException, IOException
    	  {   
    	  	String [] delimiter= new String[4];
    	  	delimiter[0] = ";";
    	  	delimiter[1] = ".";
    	  	delimiter[2] = " ";
    	  	delimiter[3] = ",";
    	    Scanner	 readUserAnswer = new Scanner(System.in);
    	    String userAnswer;
    	    
                  Group []group = new Group[3]; // 1
    	    
    	          	File f1 = new File("c:/level1.txt");
    	            BufferedReader br = new BufferedReader(new FileReader(f1));
    	   
    	          if (!f1.exists())
    	   	        {
    	   	          System.out.println ("file not found");
       	            }else
    	              {	  
       	            	String str="";
       	            	  while ( (str=br.readLine()) !=null )
       	            	   {
       	            		  setLine(str); 
       	                      segmentizeLine(getLine(), delimiter);
       	                      System.out.println ("Index is: "+getQIndex());
       	                      System.out.println ("difficulty level is:"+getDiffLevel());
       	                      System.out.println (getQuestion());
       	                      String []Options = getOptions();
       	                      
       	                   	   group[0].setGroupQues("hi");  // 2	                   	   
       	                   	   System.out.println("from the group class: "+group[0].getGroupQues()); //3
       	                   	   	  for (int i=0; i<getOptions().length; i++)
       	                           {
       	                    	     System.out.println ("Option["+(i+1)+"] =>"+Options[i]);
       	                           }
       	                   	   	     System.out.println("your answer:");
       	                   	   	     userAnswer = readUserAnswer.nextLine();
       	                   	  
       	                   	            if (isTrueAnswer(userAnswer))
       	                   	             {
       	                   	        	   System.out.println("your answer is true"); 
       	                   	             } else System.out.println("your answer is wrong");
       	            	    }
    	                
    	              }  
    	   }      
    }
    Java Code:
    package ProjectTest;
    
    public class Group {
    	
    	private String gIndex;
    	private String gDiff;
    	private String gQues;
    	private String[] gOptions;
    	private String gAnswer;
    	
    	public void setGroupIndex(String index)
    	{
    		gIndex = index;
    	}
        public String getGroupIndex()
        {
        	return gIndex;
        }
        
        public void setGroupDiff(String diff)
    	{
    		gDiff = diff;
    	}
        public String getGroupDiff()
        {
        	return gDiff;
        }
        
        public void setGroupQues(String ques)
    	{
    		gQues = ques;
    	}
        public String getGroupQues()
        {
        	return gQues;
        }
        
        public void setGroupOptions(String []options)
    	{
    		gOptions = options;
    	}
        public String[] getGroupOptions()
        {
        	return gOptions;
        }
        
        public void setGroupAnswer(String answer)
    	{
    		gAnswer = answer;
    	}
        public String getGroupAnswer()
        {
        	return gAnswer;
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Why I'm getting "java.lang.NullPointerException"

    When you get errors please post the full text of the error message.

    Does the array element that you are referencing have a valid, non null value?
    Creating an array of objects is a two step process:
    1) create the array to hold the objects (all elements are null initially)
    2) assign an object to each element

  3. #3
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    here is the full text of the erroe:


    Exception in thread "main" java.lang.NullPointerException
    at ProjectTest.ProjectTest.main(ProjectTest.java:149)

  4. #4
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    here is the full text of the erroe:


    Exception in thread "main" java.lang.NullPointerException
    at ProjectTest.ProjectTest.main(ProjectTest.java:149)

  5. #5
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    Quote Originally Posted by Norm View Post
    When you get errors please post the full text of the error message.

    Does the array element that you are referencing have a valid, non null value?
    Creating an array of objects is a two step process:
    1) create the array to hold the objects (all elements are null initially)
    2) assign an object to each element
    group[0].getGroupQues("value"); //getGroupQues is a method in the Group class, how should i have initialized it?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Why I'm getting "java.lang.NullPointerException"

    You need to assign values to all the elements in the group array that you reference.
    use an assignment statement to assign values to the elements:
    arArray[ix] = <a value>;

  7. #7
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    do u mean something like this:
    Java Code:
    package ProjectTest;
    
    public class Group {
    	
    	private String gIndex = null;
    	private String gDiff = null;
    	private String gQues = null;
    	private String[] gOptions = null;
    	private String gAnswer = null;
    	String index = null;
    	String diffLevel = null;
    	String Ques = null;
    	String []options = null;
    	String answer = null;
    	
    	public Group(String index, String diffLevel, String Ques, String []options, String answer)
    	{
    		this.index = index;
    		this.diffLevel = diffLevel;
    		this.Ques = Ques;
    		this.options = options;
    		this.answer = answer;
    	}
    	public void setGroupIndex(String index)
    	{
    		gIndex = index;
    	}
        public String getGroupIndex()
        {
        	return gIndex;
        }
        
        public void setGroupDiff(String diff)
    	{
    		gDiff = diff;
    	}
        public String getGroupDiff()
        {
        	return gDiff;
        }
        
        public void setGroupQues(String ques)
    	{
    		gQues = ques;
    	}
        public String getGroupQues()
        {
        	return gQues;
        }
        
        public void setGroupOptions(String []options)
    	{
    		gOptions = options;
    	}
        public String[] getGroupOptions()
        {
        	return gOptions;
        }
        
        public void setGroupAnswer(String answer)
    	{
    		gAnswer = answer;
    	}
        public String getGroupAnswer()
        {
        	return gAnswer;
        }
        
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Why I'm getting "java.lang.NullPointerException"

    Can you give me a hint where you mean for me to look?
    What is it I am supposed to see in your posted code?

    Your problem is that the group array is empty. Put some values in it.

    An array is like a bucket. When you buy it, there is nothing in it. You have to put something in it if you want to be able to pour anything out of it.

  9. #9
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    Quote Originally Posted by Norm View Post
    Can you give me a hint where you mean for me to look?
    What is it I am supposed to see in your posted code?
    =>private String gIndex = null;
    private String gDiff = null;
    private String gQues = null;
    private String[] gOptions = null;
    private String gAnswer = null;
    String index = null;
    String diffLevel = null;
    String Ques = null;
    String []options = null;
    String answer = null;


    Your problem is that the group array is empty. Put some values in it.
    =>it's not empty: first code line 31 group[0].setGroupQues("hi"); // 2
    line 32 System.out.println("from the group class: "+group[0].getGroupQues()); //3


    An array is like a bucket. When you buy it, there is nothing in it. You have to put something in it if you want to be able to pour anything out of it.
    is it clear??

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Why I'm getting "java.lang.NullPointerException"

    Is what clear?

    Where do you assign values to the group array?

  11. #11
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    first code: line 31 and 32

    group[0].setGroupQues("hi"); // 2
    System.out.println("from the group class: "+group[0].getGroupQues()); //3

    group array is an array of objects

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Why I'm getting "java.lang.NullPointerException"

    Where do you assign a value to group[0]? Or to any elements of the array?

    Try this to see that the first element of the array contains a null value:
    Java Code:
    System.out.println("group[0]: "+group[0]);
    Java Code:
    group[0].setGroupQues("hi")
    This statement requires that group[0] contain a Group object.
    The above println shows that it has a null value.

  13. #13
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: Why I'm getting "java.lang.NullPointerException"

    thanks alot, i solved it
    Regards,

Similar Threads

  1. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  2. Replies: 1
    Last Post: 12-13-2010, 03:19 PM
  3. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  4. Replies: 5
    Last Post: 08-26-2009, 11:37 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •