Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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:

    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
    And here's the Student class:

    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
    If someone could explain how to fix this error, it would be greatly appreciated. Thanks.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Post your error message

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    When testing your program, did you enter L first?

    If you don't enter L first, M cannot process as students[] has no data.

  4. #4
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    When testing your program, did you enter L first?

    If you don't enter L first, M cannot process as students[] has no data.
    I enter L to fill in all of the data (first name, last name, etc). The menu comes back up after all of the data is filled in and I enter M and then it crashes (java.lang.NullPointerException).

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
       while(true) {
            Student[] students = new Student[numberStudents];
    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    declare and construct the array before the while loop

    I agree.

    If you still have problems after doing that, post back with the runtime exception and the ammended driver class.

Similar Threads

  1. Swing GUI crashes (using JNI)
    By divs in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2010, 08:04 AM
  2. Multithreaded server crashes
    By skarosg3 in forum Threads and Synchronization
    Replies: 7
    Last Post: 05-26-2010, 09:24 AM
  3. Java crashes
    By Nicole in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 07:22 AM
  4. [SOLVED] NullPointerException - FileInfo Program
    By keffie91 in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:09 PM
  5. Java Crashes on Mac 10.3.9 not sure how to update
    By patricknowow in forum New To Java
    Replies: 1
    Last Post: 11-30-2007, 03:57 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
  •