Results 1 to 6 of 6
- 11-19-2010, 07:02 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Program crashes: NullPointerException
I'm making a student database and the program compiles. However, when I'm running it, the program crashes after entering "M" for modify. The line which this error is occuring is:
System.out.println(students[i].getId() + "\t" + students[i].getLastName() + "\t" +
students[i].getFirstName());
Here's the StudentDriver:
And here's the Student class:Java Code:import java.util.Scanner; public class studentDriver { public static void main() { Scanner scan = new Scanner(System.in); scan.useDelimiter ( "\n"); int numberStudents; String lastName; String firstName; String street; String city; String state; int zipCode; String homePhone; String cellPhone; double GPA; int id; int index; index = 0; id = 0; System.out.print("How many students to process? "); numberStudents = scan.nextInt(); while(true) { Student[] students = new Student[numberStudents]; System.out.println("Student Info Menu"); System.out.println("Enter L to (L)oad Student info."); System.out.println("Enter M to (M)odify Student info."); System.out.println("Enter P to (P)rint Student info."); System.out.println("Enter Q to quit."); System.out.println("Please enter your choice: "); String str; str = scan.next(); str = str.toUpperCase(); char ch = str.charAt(0); String computeQuit; switch(ch) { case 'L': for(int i = 0; i < students.length; i++) { students[i] = loadStudent(scan); } break; case 'M': for(int i = 0; i < students.length; i++) { System.out.println(students[i].getId() + "\t" + students[i].getLastName() + "\t" + students[i].getFirstName()); System.out.print("Enter the student ID: "); id = scan.nextInt(); } for(int i = 0; i < students.length; i++) { if(id == students[i].getId()) index = i; modifyStudent(students, index, scan); } break; case 'P': for(int i = 0; i < students.length; i++) { System.out.println(students[i].getId() + "\t" + students[i].getLastName() + "\t" + students[i].getFirstName()); System.out.print("Enter the student ID: "); id = scan.nextInt(); } for(int i = 0; i < students.length; i++) { if(id == students[i].getId()) index = i; printStudent(students[index]); } break; case 'Q': computeQuit = ( "Goodbye." ); str = computeQuit; System.out.println( "Goodbye." ); System.exit(0); break; } //end switcb } //end while loop } //end main public static Student loadStudent(Scanner scan) { String lastName; String firstName; String street; String city; String state; int zipCode; String homePhone; String cellPhone; double GPA; System.out.println("Enter last name: "); lastName = scan.next(); System.out.println("Enter first name: "); firstName = scan.next(); System.out.println("Enter street name: "); street = scan.next(); System.out.println("Enter city: "); city = scan.next(); System.out.println("Enter state: "); state = scan.next(); System.out.println("Enter zip code: "); zipCode = scan.nextInt(); System.out.println("Enter home phone: "); homePhone = scan.next(); System.out.println("Enter cell phone: "); cellPhone = scan.next(); System.out.println("Enter GPA: "); GPA = scan.nextDouble(); Student s = new Student(lastName, firstName, street, city, state, zipCode, homePhone, cellPhone, GPA); return s; } //end loadStudent method public static void modifyStudent(Student[]s, int idx, Scanner scan) { String lastName; String firstName; String street; String city; String state; int zipCode; String zipS; String homePhone; String cellPhone; double GPA; String GPAs; System.out.println("Current last name: " + s[idx].getLastName() + "\t" + "New last name: "); lastName = scan.next(); s[idx].setLastName(lastName); System.out.println("Current first name: " + s[idx].getFirstName() + "\t" + "New first name: "); firstName = scan.next(); s[idx].setFirstName(firstName); System.out.println("Current street name: " + s[idx].getStreet() + "\t" + "New street name: "); street = scan.next(); s[idx].setStreet(street); System.out.println("Current city: " + s[idx].getCity() + "\t" + "New city: "); city = scan.next(); s[idx].setCity(city); System.out.println("Current state: " + s[idx].getState() + "\t" + "New state: "); state = scan.next(); s[idx].setState(state); System.out.println("Current zip code: " + s[idx].getZipCode() + "\t" + "New zip code: "); zipS = scan.next(); zipCode = Integer.parseInt(zipS.trim()); s[idx].setZipCode(zipCode); System.out.println("Current home phone: " + s[idx].getHomePhone() + "\t" + "New home phone: "); homePhone = scan.next(); s[idx].setHomePhone(homePhone); System.out.println("Current cell phone: " + s[idx].getCellPhone() + "\t" + "New cell phone: "); cellPhone = scan.next(); s[idx].setCellPhone(cellPhone); System.out.println("Current GPA: " + s[idx].getGPA() + "\t" + "New GPA: "); GPAs = scan.next(); GPA = Double.parseDouble(GPAs.trim()); s[idx].setGPA(GPA); } //end modifyStudent method public static void printStudent(Student s) { System.out.println(s.toString()); } } //end class
If someone could explain how to fix this error, it would be greatly appreciated. Thanks.Java Code:import java.util.Scanner; public class Student { Scanner scan = new Scanner(System.in); private static int lastId; private int id; private String lastName; private String firstName; private String street; private String city; private String state; private int zipCode; private String homePhone; private String cellPhone; private double GPA; /***********************default constructor*****************************************************/ public Student() { id = 0; lastName = ""; firstName = ""; street = ""; city = ""; state = ""; zipCode = 0; homePhone = ""; cellPhone = ""; GPA = 0; } /***********************constructor with arguments**********************************************/ public Student(String lastName, String firstName, String street, String city, String state, int zipCode, String homePhone, String cellPhone, double GPA) { id = ++lastId; this.lastName = lastName; this.firstName = firstName; this.street = street; this.city = city; this.state = state; this.zipCode = zipCode; this.homePhone = homePhone; this.cellPhone = cellPhone; this.GPA = GPA; } //end constructors /************************************getters*****************************************************/ public int getId() { return this.id; } public String getLastName() { return this.lastName; } public String getFirstName() { return this.firstName; } public String getStreet() { return this.street; } public String getCity() { return this.city; } public String getState() { return this.state; } public int getZipCode() { return this.zipCode; } public String getHomePhone() { return this.homePhone; } public String getCellPhone() { return this.cellPhone; } public double getGPA() { return this.GPA; } //end getters /***********************************setters******************************************************/ public void setLastName(String lastName) { this.lastName = lastName; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setStreet(String street) { this.street = street; } public void setCity(String city) { this.city = city; } public void setState(String state) { this.state = state; } public void setZipCode(int zipCode) { this.zipCode = zipCode; } public void setHomePhone(String homePhone) { this.homePhone = homePhone; } public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; } public void setGPA(double GPA) { this.GPA = GPA; } //end setters } //end class
- 11-20-2010, 02:00 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Post your error message
- 11-20-2010, 05:24 AM #3
When testing your program, did you enter L first?
If you don't enter L first, M cannot process as students[] has no data.
- 11-20-2010, 09:27 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
-
You appear to be re-declaring your Student each time the while loop iterates. So even if you fill the array on the first iteration, it will be empty (nothing but nulls) on the next. You shouldn't do this, and should instead declare and construct the array before the while loop. Also, please fix your indentation as this error and others would be much easier to pick out if your code were more readable.Java Code:while(true) { Student[] students = new Student[numberStudents];
- 11-20-2010, 09:33 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Swing GUI crashes (using JNI)
By divs in forum AWT / SwingReplies: 4Last Post: 06-10-2010, 08:04 AM -
Multithreaded server crashes
By skarosg3 in forum Threads and SynchronizationReplies: 7Last Post: 05-26-2010, 09:24 AM -
Java crashes
By Nicole in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 07:22 AM -
[SOLVED] NullPointerException - FileInfo Program
By keffie91 in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:09 PM -
Java Crashes on Mac 10.3.9 not sure how to update
By patricknowow in forum New To JavaReplies: 1Last Post: 11-30-2007, 03:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks