Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:38:54+00:00 2026-05-31T02:38:54+00:00

I’m making a program in Java that reads in some stuff from a file

  • 0

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

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:38:55+00:00Added an answer on May 31, 2026 at 2:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.