1

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);
    }

}

}

Tabitha
  • 65
  • 1
  • 8
  • Just wanting to make sure - have you instantiated any of the values that you use in the first two classes? – Amndeep7 Mar 08 '12 at 23:57
  • Another question, linking both mine and @Radu 's, would it be possible for you to print out the value of `i` when you get the NPE? – Amndeep7 Mar 09 '12 at 00:01
  • In a way, @Radu is right, doing `Person people[] = new Person[N];` only creates the array but it does not create the objects of type Person. You need to initialize each element of the array. http://www.cs.usfca.edu/~wolber/courses/110/lectures/ArrayOfObjs.htm – madth3 Mar 09 '12 at 00:03
  • @Amndeep7 I believe I have, unless there is something I don't yet understand about structs. I did this in main, using //create a new instance of GiveJob GiveJob jobSearch = new GiveJob(); //Create the array to put the people into Person people[] = new Person[N]; – Tabitha Mar 09 '12 at 00:04
  • I meant in the first two classes, have you made the values in there equal to something in a constructor of some sort? – Amndeep7 Mar 09 '12 at 00:05
  • @Amndeep7 Yes, I can print out i=0 if I put a print statement in main right above //put what was read in from the file into an linked list jobSearch.GiveJob(personName, jobName, N, people); but, of course, after this it crashes – Tabitha Mar 09 '12 at 00:07
  • However, what @madth3 is saying is true - you appear to have forgotten to instantiate the values in the array. What you have done is made an array filled with `null`s, in which you can place things of class `Person`. – Amndeep7 Mar 09 '12 at 00:08
  • @Amndeep7 No, I sure haven't put any initial values in the structs. I thought that they all initialized to null. I was told an array of objects initialized to null. I'm guessing I'm way off on this? – Tabitha Mar 09 '12 at 00:10
  • In Java, unlike in C which I think you're coming here from, when you do something like this `ClassName cn;`, you do not get a pointer pointing to an object of type `ClassName` with values within set to null, you just get a pointer to something that is null. – Amndeep7 Mar 09 '12 at 00:13
  • Would you like to see if we can discuss all of this in the chat feature they have available? Actually it appears that you can't access that yet. – Amndeep7 Mar 09 '12 at 00:14
  • @Amndeep7 What you have done is made an array filled with nulls, in which you can place things of class Person -- But wait, this is what I meant to do (I think). I am making an array filled with nulls, then I check: if people[i] is null, fill it in. Am I going about this completely the wrong way? – Tabitha Mar 09 '12 at 00:14
  • @Amndeep7 Unfortunately, I don't have enough reputation to chat :( I'm just getting started in this kind of stuff. I do realize what you are saying, though. The whole array is initialized to null, not the individual elements. That makes sense. I guess I should make a constructor of some kind that will iterate through my array and initialize each element to null? Does that sound like it will work to allow me to compare people[i] == null like I want? – Tabitha Mar 09 '12 at 00:20
  • This will look like it requires an extensive response. Please give me a few minutes to make an answer. – Amndeep7 Mar 09 '12 at 00:23
  • Just wanted to say that I have answered, along with numerous other people. Please either ask for more clarification or choose an answer as your preferred one. – Amndeep7 Mar 09 '12 at 00:51

5 Answers5

4

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 name are not null, you are going to have to instantiate your Person class somewhere. You seem to want to do this in the GiveJob method (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 of people[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. null does not have a jobs variable 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.

Amndeep7
  • 1,764
  • 4
  • 15
  • 23
  • Thanks a ton - this is a HUGE help. Good catch on the missing type declaration in GiveJob, by the way :-) – Tabitha Mar 09 '12 at 01:03
  • And, by the way, you are right about me coming from a C background, lol. – Tabitha Mar 09 '12 at 01:08
  • No problem, thanks for that green checkmark over there - its greatly appreciated. – Amndeep7 Mar 09 '12 at 01:09
  • And anyways, it was easy enough to see those C influences - I'm taking a class that uses C so I was having basically the same problems you're having except in reverse - lol. – Amndeep7 Mar 09 '12 at 01:11
1
Person people[] = new Person[N];

This just creates an array of size N filled with NULL. I do not see any initialization code before you call people[i].job in GiveJob function. Something like this,

for(int i=0; i<N ;i++ ){
   people[i] = new Person();
}
RandomQuestion
  • 6,176
  • 16
  • 52
  • 91
  • This is an incomplete solution. If you looked at the comments we've gotten to so far, it appears that not just Person instantiations are empty, but also the values within the class. – Amndeep7 Mar 09 '12 at 00:15
  • Thanks, this little code snippet was extremely helpful. – Tabitha Mar 09 '12 at 01:10
0
  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

Rather than that you should go through your array like this:

      for(int i=0; i<people.length; i++){
                  if(people[i].jobs != null){    
}

And in that row the comma is missing also:

public void GiveJob(String personName, String newJob int N, Person[] people)

After String , before int

czupe
  • 4,212
  • 6
  • 28
  • 51
0

A simple solution would be to change the order of your if conditions so you first check whether an array element is null, and if so, initialize it by assigning a new Person instance like so: people[i] = new Person()

Fixed code:

   if(people[i] == null){
        people[i] = new Person(); // ADD THIS LINE TO FIX THE NPE
        people[i].name = personName;
        Jobs tempNode = new Jobs();
        tempNode.typeOfJob = newJob;
        tempNode.next = null;
        people[i].jobs = tempNode;
        break;
    } else if(people[i].jobs != null){
        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
    }

You need to do this because the elements of the Array you created with Person people[] = new Person[N]; are still null, so the memory for the properties you are trying to access is not yet allocated. Java doesn’t have anything similar to C structs, so your struct is just a simple Java Object, which needs to be instantiated with new, before its fields can be accessed.

SQiShER
  • 925
  • 6
  • 10
0

swap the if around: first check if the person has been added and then check it's job:

for(int i=0; i<people.length; i++){
    //if the person has not been added yet, add him/her
    if(people[i] == null){
        people[i] = new Person();
        people[i].name = personName;
        Jobs tempNode = new Jobs();
        tempNode.typeOfJob = newJob;
        tempNode.next = null;
        people[i].jobs = tempNode;
        break;
    }        

    //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
    }
}
ratchet freak
  • 44,814
  • 5
  • 55
  • 99