Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>Person</code> instance like so: <code>people[i] = new Person()</code></p> <p>Fixed code:</p> <pre><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 } </code></pre> <p>You need to do this because the elements of the Array you created with <code>Person people[] = new Person[N];</code> 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 <em>struct</em> is just a simple Java Object, which needs to be instantiated with new, before its fields can be accessed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload