从数组获取元素时发生NullPointerException

时间:2019-03-24 17:49:56

标签: java

我正在尝试从姓名和身份证号码列表中打印出员工的姓名及其部门和位置,即使打印了所有姓名和位置,我仍然会收到NullPointerException。然后,它将停止构建,并且不会执行打印部门和打印位置方法。

我尝试重做for循环,看看是否有任何数据点是问题,但是如果我对所有Employee对象执行循环,或者只需做一个。

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

        }
    }
}

.txt文件的使用量很小:

Alexander
Seiber
10010
Zehua
Showalter
20010
Cassidy
Woodle
20030
Randall
Shaukat
10030
Pam
Korda
10020
Justin
Polito
20030

2 个答案:

答案 0 :(得分:1)

public static int emplIndex = -1;

为什么索引保持为static字段?不要那样做。

Employee[] emp = new Employee[50];

员工数组的固定大小为50个元素,

while (scan.hasNextLine()) {

此循环基于acmeEgr.txt文件的各行,它们可能比50多。
在这种情况下,您将首先获得ArrayOutOfBoundException

emp[emplIndex] = new Employee();

或之后的NullPointerException

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

相反,如果行少于 50,则

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

仍将循环所有50元素,因为

ppl.length = 50

因此,这一行

ppl[i].toString()

将抛出NullPointerException
如果元素为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!

答案 1 :(得分:0)

ArrayList是更易于处理的数组类型。尝试使用它而不是普通数组,因为那样就不必处理索引了。