Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default NullPointerException help

    I am working on a project on Student Score management system.
    When I am running the program to start the bubblesort after entering the students information, NullPointerException occur.
    I know this is due to the null value of array.
    But I don't know how to fix it because I should have entered the students' records and the "list" array should be present and contain value. Why it still appears as null ??

    This is the error:
    Exception in thread "main" java.lang.NullPointerException
    at Students.bubbleSort(Students.java:121)
    at Students.main(Students.java:108)


    Java Code:
    import java.util.*;
    public class Students 
    {
    
    	private String id,fn,ln;
    	private int age;
    	private char gender;
    	private double feg;
    	static Students list[];
    	static int select,number;
    	
    		
    	public Students()
    	{
    	}
    	
    	public Students(String a,String b, String c, int d,char e, double f)
    	{
    		this.id=a;
    		this.fn=b;
    		this.ln=c;
    		this.age=d;
    		this.gender=e;
    		this.feg=f;
    	}
    
    	public String getID()
    	{
    		return this.id;
    	}
    	
    	public double getFeg()
    	{
    		return this.feg;
    	}
    	
    	public String getName()
    	{
    		return this.fn+" "+this.ln;
    	}
    	
    		
    	public static void main(String []args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Press 1 to input student information");
    		System.out.println("Press 2 to list all students according to final grades");
    		System.out.println("Press 3 to search for a specific student's information");
    		select = keyboard.nextInt();
    		if(select==1)
    		{
    			System.out.println("How many students' information you want to enter?");
    			number=keyboard.nextInt();
    			
    			Students  list [] =new Students[number];
    			
    			for(int i=0;i<list.length;i++)
    			{
    				String ida,fna,lna;
    				int agea;
    				char gendera;
    				double fega;
    								
    				
    				System.out.println("Input id");
    				ida=keyboard.next();
    				System.out.println("Input First Name");
    				fna=keyboard.next();
    				System.out.println("Input Last Name");
    				lna=keyboard.next();
    				System.out.println("Input Age");
    				agea=keyboard.nextInt();
    				System.out.println("Input Gender");
    				gendera=keyboard.next().charAt(0);
    				System.out.println("Input Score");
    				fega=keyboard.nextInt();
    				list[i]= new Students(ida,fna,lna,agea,gendera,fega);
    			}
    					
    			
    			for(int j=0;j<list.length;j++)
    			{
    				System.out.println(list[j]+list[j].getID());
    			}
    		}
    		
    		System.out.println("Succeed to input "+number+" records.");
    		System.out.println(" ");
    		System.out.println("Press 2 to list all students according to final grades");
    		System.out.println("Press 3 to search for a specific student's information");
    		
    		
    				
    		select = keyboard.nextInt();
    		if(select ==2)
    		{					
    			bubbleSort(list);
    
    			for (int i=0; i < list.length; i++) 
    				{
    				System.out.println( list[i].getName() + " " +
    					list[i].getFeg() );
    				}
    		}
    		
    	}
    	
    	public static void bubbleSort(Students [] array)
    	{
    		for (int pass=0; pass < array.length-1; pass++) 
    			for (int k=0; k < array.length-pass-1; k++) 
    				if (array[k].getFeg() > array[k+1].getFeg())
    					swap(array, k, k+1);
    	}
    
    	public static void swap(Students [] array, int first, int second)
    	{
    		Students	temp;
    
    		temp = array[first];
    		array[first] = array[second];
    		array[second] = temp;
    	}
    
    	
    	
    }

  2. #2
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerException help

    The line generating error should be 97 and 110

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: NullPointerException help

    You have a static array 'list' in line #9, but you also have a local one (which hides the other one) in line #55; don't create a local one in line #55 but initialize the static one there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerException help

    Thank you for your reply!! But I want to let the user to create the array with their own size. Is there any way that I can do this?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: NullPointerException help

    Quote Originally Posted by laichunkitjacky View Post
    Thank you for your reply!! But I want to let the user to create the array with their own size. Is there any way that I can do this?
    Sure you can, just don't introduce a local array at line #55, i.e. change it to:

    Java Code:
    list [] =new Students[number];
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. NullPointerexception
    By danewithoutwax in forum New To Java
    Replies: 4
    Last Post: 11-26-2011, 12:51 PM
  2. I keep getting NullPointerException
    By coasterguy10 in forum New To Java
    Replies: 6
    Last Post: 09-19-2011, 05:53 PM
  3. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  4. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 06:18 AM
  5. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-18-2008, 11:34 PM

Tags for this Thread

Posting Permissions

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