NullPointerException

Exception in thread "main" java.lang.NullPointerException

at CollegeList.main(CollegeList.java:38)

This is the error i'm recieving. I think its something to do with an array being empty, but i'm not sure how to give this array values with the guidelines i've been set (this is an assignment) according to the question i have to answer, i think ive covered everything. I was hoping some of you out there could give me some pointers on how to fix this, and i'll see if i'm allowed to do it.

Heres all my code, theres a couple of classes i wont paste here since they aren't relevant :

import java.util.Scanner;

import javax.swing.*;

publicclass CollegeList{

publicstaticvoid main(String[] args){

String ssNum1;

String salary1;

String depName1;

Scanner sc =new Scanner(System.in);

CollegeEmployee[] employees =new CollegeEmployee[4];

//Faculty[] facMember = new Faculty[3];

//Student[] students = new Student[7];

char choice;

System.out.println("Which users data would you like to enter? (C = College Employee, F = Faculty member, S = Student, Q = Quit");

choice = sc.next().charAt(0);

if(choice =='C' || choice =='c'){

System.out.println("College Employees");

for(int x = 0; x < employees.length; x++){

employees[x].setData();

employees[x].display();

}

}

//if(choice == 'F' || choice == 'f') {} //(ive made it so you can only enter C as an option to make things simpler at the moment)

//if(choice == 'S' || choice == 's') {}

//if(choice == 'Q' || choice == 'q') {}

}

import javax.swing.*;

publicclass CollegeEmployeeextends Person{

String ssNum;

String salary;//for input, no reason to change to double.

String depName;

publicvoid setData(){

ssNum = JOptionPane.showInputDialog(null,"Enter Social Security Number");

depName = JOptionPane.showInputDialog(null,"Enter Department Name");

salary = JOptionPane.showInputDialog(null,"Enter annual salary");

}

publicvoid display(){

System.out.println("Social Security Number: " + ssNum);

System.out.println("Annual salary: " + salary);

System.out.println("Department name: " + depName);

}

}

import javax.swing.*;

publicclass Person{

String firstName;

String lastName;

String address;

String zipCode;//Zip code is a string because it was easier to keep it the same type as the other variables, but normally it would be an int

String phNum;//Phone number is a string incase number starts with 0

publicvoid setData(){

firstName = JOptionPane.showInputDialog(null,"Enter first name");

lastName = JOptionPane.showInputDialog(null,"Enter last name");

address = JOptionPane.showInputDialog(null,"Enter Address");

zipCode = JOptionPane.showInputDialog(null,"Enter Zip code");

phNum = JOptionPane.showInputDialog(null,"Enter phone number");

}

publicvoid display(){

System.out.println("First name: " + firstName);

System.out.println("Last name: " + lastName);

System.out.println("Address: " + address);

System.out.println("Zip code: " + zipCode);

System.out.println("phNum: " + phNum);

}

}

Thanks in advance guys

And just so you know, line 38 is : employees[x].setData();

[5676 byte] By [Adam123a] at [2007-11-27 4:30:20]
# 1
You have created the array, but it doesn't mean that the elements inside it have been initialised. In order to achieve this you should just add employees[x] = new CollegeEmployee(); as first line of your for loop.
Peetzorea at 2007-7-12 9:39:34 > top of Java-index,Java Essentials,New To Java...
# 2
of course!! thanks heaps, that completely fixed the problem. can't believe i didn't realise that before.thanks again.===========RESOLVED===========
Adam123a at 2007-7-12 9:39:34 > top of Java-index,Java Essentials,New To Java...