-1

I'm trying to print out the names of the employees and their department and location from a list of the names and id numbers and I keep getting the NullPointerException even though it prints all of the names and locations. It then stops the build and doesn't xecute the print department and print location methods.

I've tried re-doing the for loops and seeing if any one data point was the problem but it seems to happen if I do the loop for all of the Employee objects or if I just do one.

package homework5_parth_desai;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 *
 * @author Party Parth
 */
public class Homework5_Parth_Desai {

    public static int emplIndex = -1;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("acmeEgr.txt");
        Scanner scan = new Scanner(file);
        Employee[] emp = new Employee[50];
        String s = "";
        String t = "";
        int r = 0;

        while (scan.hasNextLine()) {    //scans in file 
            emplIndex++;
            emp[emplIndex] = new Employee();
            if (scan.hasNextLine() == true) {    //takes first line as first name, second as last naem and third as id number and tehn ccreates an object out of that
                s = scan.nextLine();                   
            }
            if (scan.hasNextLine() == true) {
                t = scan.nextLine();
            }
            if (scan.hasNextLine() == true) {
                r = Integer.parseInt(scan.nextLine());
            }

            emp[emplIndex].Employee(s, t, r);

            // TODO code application logic here
        }

        printAll(emp);

        printDepartment("IT", emp);

        printLocation("Auburn Hills", emp);

    }

    static void printAll(Employee[] ppl) {
        for (int i = 0; i < ppl.length; i++) {
            System.out.println(ppl[i].toString());
        }
    }

    static void printDepartment(String title, Employee[] ppl) {
        for (int i = 0; i < ppl.length; i++) {
            if (title.equals(ppl[i].getDept())) {
                System.out.println(ppl[i].getName() + " is in " + ppl[i].getLocation());
            }
        }
    }

    static void printLocation(String loc, Employee[] ppl) {
        for (int i = 0; i < ppl.length; i++) {
            if (loc.equals(ppl[i].getLocation())) {
                System.out.println(ppl[i].getName() + "  is in " + ppl[i].getDept());
            }

        }
    }
}

Small exert of the .txt file:

Alexander
Seiber
10010
Zehua
Showalter
20010
Cassidy
Woodle
20030
Randall
Shaukat
10030
Pam
Korda
10020
Justin
Polito
20030
LppEdd
  • 20,274
  • 11
  • 84
  • 139

2 Answers2

1
public static int emplIndex = -1;

Why is the index maintained as a static field? Don't do that.

Employee[] emp = new Employee[50];

The employee array has a fixed size of 50 elements, however

while (scan.hasNextLine()) {

this loop is based on the lines of the acmeEgr.txt file, which might be more than 50.
In that case, you'll get an ArrayOutOfBoundException first

emp[emplIndex] = new Employee();

or a NullPointerException after

emp[emplIndex].Employee(s, t, r);

Instead, if the lines are less then 50, this

for (int i = 0; i < ppl.length; i++) {
   System.out.println(ppl[i].toString());
}

will still loop all the 50 elements, because

ppl.length = 50

Thus, this line

ppl[i].toString()

will throw a NullPointerException.
This is what happens if the elements are, for example, 40

System.out.println(ppl[0].toString());
System.out.println(ppl[1].toString());
System.out.println(ppl[2].toString());
System.out.println(ppl[3].toString());
...
System.out.println(ppl[40].toString()); // ppl[40] is null, NullPointerException!
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • thank you very much, the file only had 41 people so i used the static int empIndex as the end of my for loops instead of ppl.length and now she works to prfection. Sad it took me 3 hours and the interent to figure that one out – Parth Desai Mar 24 '19 at 18:19
0

ArrayList is a much easier array type to deal with. Try using it instead of a normal array, because then you don't have to deal with indexes.

Mr. Arbitrary
  • 932
  • 11
  • 25