Results 1 to 5 of 5
Thread: NullPointerException help
- 12-04-2011, 03:15 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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; } }
- 12-04-2011, 04:26 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: NullPointerException help
The line generating error should be 97 and 110
- 12-04-2011, 04:32 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-04-2011, 04:47 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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?
- 12-04-2011, 05:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
NullPointerexception
By danewithoutwax in forum New To JavaReplies: 4Last Post: 11-26-2011, 12:51 PM -
I keep getting NullPointerException
By coasterguy10 in forum New To JavaReplies: 6Last Post: 09-19-2011, 05:53 PM -
NullPointerException
By Diz in forum New To JavaReplies: 10Last Post: 05-13-2011, 02:58 AM -
I get a NullPointerException and don't know why
By hendrix79 in forum New To JavaReplies: 9Last Post: 12-14-2008, 06:18 AM -
NullPointerException
By Aika in forum New To JavaReplies: 8Last Post: 11-18-2008, 11:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks