2 Replies - 1129 Views - Last Post: 04 April 2013 - 06:17 PM Rate Topic: -----

#1 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

NullPointException help with a Null value

Posted 03 April 2013 - 12:43 AM

ok so i have a main method/class to call other classes to input and output variables i am well aware of the issue of group1 being a null i am just here to request help as to a snippet or help on how to have it return a value preventing it from just closing on its self.

I am just requesting assistance with this code if there is anyone out there who can help if nothing makes sense then please post down below if there is anything you need

error

Exception in thread "main" java.lang.NullPointerException
	at CollegeList.main(CollegeList.java:35)

This error was the first input for Firstname

                System.out.println("Enter first name: ");
                fname = scan.nextLine();
                group1[i].setFirstName(fname); 

Being that i have searched the issue and still to find the fix for this issue i am just here to ask if there is anyone that is able to assist me with this issue at hand.




import java.util.Scanner;

public class CollegeList {
   public static void main(String[] args) {
        CollegeEmployee[] group1 = new CollegeEmployee[4];
        Faculty[] group2 = new Faculty[3];
        Student[] group3 = new Student[7];
        Scanner scan = new Scanner(System.in);
        String response;
        String fname;
        String lname;
        String address;
        String zip;
        String phone;
        String cont ="Y";
        String QUIT = "Q";

        System.out.println("(C)ollege employee, (F)aculty or (S)tudent, (" + QUIT + ") to quit:");
        response = scan.nextLine();

        switch (response) {
            case "C":
            while(cont.equals("Y")) {
                int i = 0;
	 
                System.out.println("Enter first name: ");
                fname = scan.nextLine();
                group1[i].setFirstName(fname);

                System.out.println("Enter last name: ");
                lname = scan.nextLine();
                group1[i].setLastName(lname);

                System.out.println("Enter address: ");
                address = scan.nextLine();
                group1[i].setAddress(address);

                System.out.println("Enter zip code: ");
                zip = scan.nextLine();
                group1[i].setZipCode(zip);

                System.out.println("Enter phone number: ");
                phone = scan.nextLine();
                group1[i].setPhoneNumber(phone);

                System.out.println("Enter more? (Y/N)");
                cont = scan.nextLine();

                i++;
            }
				
				case "F":
            while(cont.equals("Y")) {
                int x = 0;
					
                System.out.println("Enter first name: ");
                fname = scan.nextLine();
                group2[x].setFirstName(fname);

                System.out.println("Enter last name: ");
                lname = scan.nextLine();
                group2[x].setLastName(lname);

                System.out.println("Enter address: ");
                address = scan.nextLine();
                group2[x].setAddress(address);

                System.out.println("Enter zip code: ");
                zip = scan.nextLine();
                group2[x].setZipCode(zip);

                System.out.println("Enter phone number: ");
                phone = scan.nextLine();
                group2[x].setPhoneNumber(phone);

                System.out.println("Enter more? (Y/N)");
                cont = scan.nextLine();

                x++;
            }
				
				case "S":
            while(cont.equals("Y")) {
                int z = 0;
					
                System.out.println("Enter first name: ");
                fname = scan.nextLine();
                group2[z].setFirstName(fname);

                System.out.println("Enter last name: ");
                lname = scan.nextLine();
                group2[z].setLastName(lname);

                System.out.println("Enter address: ");
                address = scan.nextLine();
                group2[z].setAddress(address);

                System.out.println("Enter zip code: ");
                zip = scan.nextLine();
                group2[z].setZipCode(zip);

                System.out.println("Enter phone number: ");
                phone = scan.nextLine();
                group2[z].setPhoneNumber(phone);

                System.out.println("Enter more? (Y/N)");
                cont = scan.nextLine();

                z++;
            }

        }
    }
}


Thankyou
Kind Regards,
Baron

This post has been edited by LawlessBaron: 03 April 2013 - 12:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: NullPointException help with a Null value

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,810
  • Joined: 12-December 12

Re: NullPointException help with a Null value

Posted 03 April 2013 - 03:14 AM

CollegeEmployee[] group1 = new CollegeEmployee[4];

This line creates an array capable of storing four CollegeEmployees, but it does not create these employees. Within your loop you still need to instantiate each employee:

    group1[i] = new CollegeEmployee();

Was This Post Helpful? 2
  • +
  • -

#3 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: NullPointException help with a Null value

Posted 04 April 2013 - 06:17 PM

View Postandrewsw, on 03 April 2013 - 06:14 PM, said:

CollegeEmployee[] group1 = new CollegeEmployee[4];

This line creates an array capable of storing four CollegeEmployees, but it does not create these employees. Within your loop you still need to instantiate each employee:

    group1[i] = new CollegeEmployee();

Thankyou man sorry for the long time to respond i have only just been able to jump on thanks heaps mate

This post has been edited by LawlessBaron: 04 April 2013 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1