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 😛
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);
}
}
}
It appears that there are numerous things wrong with your code – unfortunately your attempt to reducing the amount of code posted in your question (which is usually a good thing) also has backfired in that it makes it more difficult to determine what are errors in your code and what are just errors from when you copy-pasted stuff in.
Just one note before I start explaining some stuff to you, you might want to look into some websites or other places that help the transition over from the C language to Java as you are getting some fundamental things wrong. This is not taking fault with your programming ability, you just do not know some of the things that set Java apart from C, such as how to do conditionals.
Anyways, what I think the problem is is that the values in your classes and in fact the classes themselves have not yet been initialized/instantiated (you have named them, in that you are now capable of setting values to them and so on, but they do not actually have any values yet). Let us take care of small matters first, you will need to instantiate the values in your classes like Jobs and Person. The way to do this is through a constructor (please just take a look at an introduction to Java programming website or book if you are not certain about the things I’m talking about). After you have made sure that the values of things like
nameare not null, you are going to have to instantiate yourPersonclass somewhere. You seem to want to do this in theGiveJobmethod (which is named in an inappropriate style and also is missing a type declaration for one of the arguments). Therefore you’re going to have to instantiate Person objects (something along the lines ofpeople[i] = new Person(/*args*/)).The particular problem that you’re asking to be solved lies in the fact that you do not have an instantiated Person in your array of Persons yet. However, you are trying to access (once again in an inappropriate manner) a variable from that Person. This person does not exist, it is of type
null.nulldoes not have ajobsvariable that belongs to it. Therefore, you get a Null Pointer Error.@ratchet freak and @SQiShER have the way to fix it displayed appropriately. However, you are still going to want to look at basic Java coding guides to help you get adjusted to the new situation and the appropriate stylistic ways to do things.