I'm making a program in Java that reads in some stuff from a file and stores everything in an array. Each slot in the array is a linked list. I'm getting a Null Pointer Exception and I don't know why. I'm pretty new at programming, and I have an awful feeling it's something obvious that I don't see, but it will take everyone who looks at it, oh, I don't know, maybe two seconds to figure it out... then I will get to feel stupid, but anyway, here goes...
The NPE, according to my debugger (I'm using Eclipse), is in the GiveJob class. I have marked the line with all caps to ease in finding it.
My first thought about the NPE is that it must have something to do with the fact that I have an array of structs. To my understanding, every slot in an array of objects is automatically initialized to null when using Java, and I thought that this would include an array of structs. Am I wrong?
Any help is very greatly appreciated, as I have been puzzling over this for quite some time now :-P
Here is the class for the array:
public class Person{
String name;
Jobs jobs;
}
Here is the class for the linked list:
public class Jobs{
String typeOfJob;
Jobs next;
}
Here is the class for giving the person a job:
public void GiveJob(String personName, String newJob int N, Person[] people){
//go through the array of people to see if the person already exists
for(int i=0; i<N; i++){
//check to see if the person has already been added
if(people[i].jobs != null){ //NULL POINTER EXCEPTION
if(people[i].jobs.compareToIgnoreCase(newJob) == 0){
//if the person has been added, check to see if the job has
//already been added
Jobs currentNode = people[i].jobs;
while(currentNode.next != null){
//if the job has already been added, break
if(currentNode.typeOfJob.compareToIgnoreCase(newJob) == 0){
break;
}
currentNode = currentNode.next;
}
//if the job has already been added, break
if(currentNode.typeOfJob.compareToIgnoreCase(newJob) == 0){
break;
}
else{
Jobs tempNode = new Jobs();
tempNode.typeOfJob = newJob;
tempNode.next = null;
people[i].jobs.next = tempNode;
}
}//end if the job has already been added
}
//if the person has not been added yet, add him/her
else if(people[i] == null){
people[i].name = personName;
Jobs tempNode = new Jobs();
tempNode.typeOfJob = newJob;
tempNode.next = null;
people[i].jobs = tempNode;
break;
}
}//end for(int i=0; i<N; i++) - checking if the city has been added already
}//end addToAdjList method
}//end AdjacencyList class
Here is the class containing main:
import java.io.*;
import java.util.*;
public class LookingForAJob {
public static void main(String[] args) {
//read in file
try{
File filename = new File("jobListing.txt");
Scanner fin = new Scanner(filename);
//read in the number of people (N) from file
int N = fin.nextInt();
//read in the number of jobs available (M) from file
int M = fin.nextInt();
//create a new instance of GiveJob
GiveJob jobSearch = new GiveJob();
//Create the array to put the people into
Person people[] = new Person[N];
//read in information from file
for(int i=0; i<M; i++){
//get person's name
String personName = fin.next();
//get job name
String jobName = fin.next();
//put what was read in from the file into an linked list
jobSearch.GiveJob(personName, jobName, N, people);
}//end for(int i=0; i<M; i++)
}//end try
catch(FileNotFoundException e){
System.err.println("Input file not found for reading!");
System.exit(0);
}
catch(Exception e){
System.err.println("Input file not in correct format");
System.exit(0);
}
}
}