Results 1 to 3 of 3
  1. #1
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Null pointer exception

    please help me to solve this exception

    Java Code:
    package p1;
    import java.io.*;
    import java.util.Scanner;
    
    public class SortObject {
    	int age;
    	String name,gender;
    	
    		public static void main(String[] args) throws IOException
    		{
    			int a;
    		//	BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
    	      
    			SortObject[] s=new SortObject[10];
    			SortObject ele=new SortObject();
    			//taking input from user
    			try
    			{
    				 Scanner sc=new Scanner(System.in);
    				for(int i=0;i<10;i++)
    				{
    					System.out.println("enter details of "+(i+1)+"th person");
    					s[i].age=sc.nextInt();
    					s[i].name=sc.next();
    					s[i].gender=sc.next();
    				}
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    			
    			//printing array element
    			for(int i=0;i<10;i++)
    			{
    				System.out.println("Name="+s[i].name+"\t Age="+s[i].age+"\t Gender="+s[i].gender);
    			}
    			
    			//sorting array elements
    		     for(int i=0;i<10;i++)
                 {
                         for(int j=0;j<9-i;j++)
                         {
                                 if(s[j].age>s[j+1].age)
                                 {
                                         ele=s[j];
                                         s[j]=s[j+1];
                                         s[j+1]=ele;
                                   
                                 }
    
                         }
                 }
    		     
    		     System.out.println("SORTED DETAILS");
    		     
    		     for(int i=0;i<10;i++)
    		     {
    		    	 System.out.println("Age="+s[i].age+"\t Name="+s[i].name+"\t Gender="+s[i].gender);
    		     }
    		     
    
    			
    		}
    	
    
    }
    /*
    enter details of 1th person
    10
    java.lang.NullPointerException
    	at p1.SortObject.main(SortObject.java:23)
    Exception in thread "main" java.lang.NullPointerException
    	at p1.SortObject.main(SortObject.java:36)
    
    */

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Null pointer exception

    SortObject[] s=new SortObject[10] creates an array with room for ten items, but they all are still NULL. You have to do something like s[i] = new SortObject() inside the loop first.

    Edit: better example.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Null Pointer Exception
    By Army in forum New To Java
    Replies: 19
    Last Post: 01-19-2013, 05:14 AM
  2. Need Help With Null Pointer Exception
    By kendraheartt in forum New To Java
    Replies: 4
    Last Post: 08-16-2012, 06:29 PM
  3. Null pointer Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-21-2012, 12:38 AM
  4. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 01:37 AM
  5. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 02:12 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
  •