java nullpointerexception错误,请帮帮我

时间:2013-12-13 01:01:44

标签: java nullpointerexception

我编写了这段代码,除了一件事,一切正常。 在方法whatIsThebestEmployeeName我有问题将字符串从一个对象复制到另一个对象。它告诉我nullpointerexception。 赞赏你的帮助:)

enter code here
import java.util.Scanner;

public class HW3 {
public static class Employee {
    private int id;
    private String name;
    private int salary;

    public Employee(int id_num, String n, int s) {
        id = id_num;
        name = n;
        salary = s;
    }

    public String toString() {
        return id + " " + name + " " + salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    public int getId() {
        return id;
    }

    public void setName(int id_num) {
        id = id_num;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int s) {
        salary = s;
    }
}

public static class bestName {
    private String name;
    private double avg;

    public bestName(String s, double n) {
        name = s;
        avg = n;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    public String toString() {
        return "Best name: " + name + "\n" + "avg = " + avg;
    }
}

public static Employee getOneEmployee(int i) {
    Scanner s = new Scanner(System.in);
    System.out.println("Please Enter Employee's #" + i + " id:");
    int id = s.nextInt();
    s.nextLine();
    System.out.println("Please Enter Employee's #" + i + " name:");
    String name = s.nextLine();
    System.out.println("Please Enter Employee's #" + i + " salary:");
    int salary = s.nextInt();
    Employee temp = new Employee(id, name, salary);
    return temp;
}

public static Employee[] getEmployeesArray(int n) {
    Employee[] arr = new Employee[n];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = getOneEmployee(i + 1);
    }
    return arr;
}

public static void printEmployeesArray(Employee[] arr) {
    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}

public static void swap(Employee[] arr, int from, int to) {

    Employee temp = arr[from];
    arr[from] = arr[to];
    arr[to] = temp;
}

public static void bubbleSort(Employee[] arr) {
    boolean bChanged = true;
    for (int i = arr.length - 1; i > 0 && bChanged; i--) {
        bChanged = false;
        for (int j = 0; j < i; j++) {
            if (arr[j].name.compareTo(arr[j + 1].name) > 0) {
                swap(arr, j, j + 1);
                bChanged = true;
            } else if ((arr[j].name.compareTo(arr[j + 1].name) == 0)
                    && (arr[j].salary > arr[j + 1].salary)) {
                swap(arr, j, j + 1);
                bChanged = true;
            }
        }
    }
}

public static int howManyRows(Employee[] arr) {
    if (arr.length == 1)
        return 1;
    int count = 0;
    int i = 0;
    Employee temp = arr[i];
    for (i = i + 1; i < arr.length; i++) {
        if (!(temp.name.equals(arr[i].name)))
            count++;
    }
    if ((count == 0) || (count == 1))
        return ++count;
    else
        return count;
}

public static int howManyColumns(Employee[] arr, int k) {
    if (arr.length == 1)
        return 1;
    int count = 1;
    Employee temp = arr[k];
    for (int i = k + 1; i < arr.length; i++) {
        if (temp.name.equals(arr[i].name))
            count++;
        else
            break;
    }

    return count;
}

public static Employee[][] setEmployeeMatrix(Employee[] arr) {
    int rows = howManyRows(arr);
    Employee[][] employeeMat = new Employee[rows][];
    int k = 0;
    for (int i = 0; i < employeeMat.length; i++) {
        int columns = howManyColumns(arr, k);
        employeeMat[i] = new Employee[columns];
        for (int j = 0; j < employeeMat[i].length; j++) {
            employeeMat[i][j] = arr[k];
            k++;
        }

    }
    return employeeMat;
}

public static void printEmployeeIdMatrix(Employee[][] mat) {
    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat[i].length; j++) {
            System.out.print("  " + mat[i][j].id);
        }
        System.out.println();
    }
}

public static double getAvg(Employee[][] arr, int i) {
    double avg = 0;
    for (int j = 0; j < arr[i].length; j++)
        avg += arr[i][j].salary;
    return avg / arr.length;
}

public static bestName bestAvg(bestName[] arr) {
    int k = 0;
    double max = arr[k].avg;
    for (int i = ++k; i < arr.length; i++) {
        if (arr[i].avg > max) {
            max = arr[i].avg;
            k = i;
        }
    }
    return arr[k];
}

public static bestName whatIsThebestEmployeeName(
        Employee[][] employeeMatrix, Employee[] employeeArr) {
    bestName[] arr = new bestName[employeeMatrix.length];
    for (int i = 0; i < arr.length; i++) {
        arr[i].name = employeeArr[i].name;
        arr[i].avg = getAvg(employeeMatrix, i);
    }
    return bestAvg(arr);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner s = new Scanner(System.in);
    System.out.println("Please Enter how many Employees:");
    int n = s.nextInt();
    Employee[] employeeArr = getEmployeesArray(n);
    System.out.println("Original array from user:\n");
    printEmployeesArray(employeeArr);
    System.out.println("\nArray after sorting:\n");
    bubbleSort(employeeArr);
    printEmployeesArray(employeeArr);
    System.out.println("\nMatrix:");
    Employee[][] employeeMatrix = setEmployeeMatrix(employeeArr);
    printEmployeeIdMatrix(employeeMatrix);
    bestName bestEmployeeName = whatIsThebestEmployeeName(employeeMatrix,
            employeeArr);
    System.out.println(bestEmployeeName);
    s.close();
}

}

2 个答案:

答案 0 :(得分:0)

您需要先向数组添加new bestNames,然后才能尝试更改数据。

    bestName[] arr = new bestName[employeeMatrix.length];  // all null

    arr[i].name = employeeArr[i].name;
    arr[i].avg = getAvg(employeeMatrix, i);

第一行代码创建一个nestName类型的数组,其中包含所有null个值。您需要先使用new bestName初始化所有索引,然后才能更改arr[i].namearry[i].avg

也许你想要像

这样的东西
public static bestName whatIsThebestEmployeeName(
        Employee[][] employeeMatrix, Employee[] employeeArr) {
    bestName[] arr = new bestName[employeeMatrix.length];
    for (int i = 0; i < arr.length; i++) {
        array[i] = new bestName();             <=-------- Change --------<<<<
        arr[i].name = employeeArr[i].name;
        arr[i].avg = getAvg(employeeMatrix, i);
    }
    return bestAvg(arr);
}

另外,我刚注意到bestName的字段是私有的。您可能想要使用它的构造函数。

for (int i = 0; i < arr.length; i++) {
     String name = employeeArr[i].getName();   // notice getName() change
     double avg = getAvg(employeeMatrix, 1);
     array[i] = new bestName(name, avg);             
 }

请注意getName()更改。因为你所有的领域都是私人的。您希望确保尝试使用getMethods()访问数据并使用setMethods()或通过构造函数设置其值。您应该查看所有代码以确保正确无误。

答案 1 :(得分:0)

在尝试设置其属性之前必须实例化arr[i]

arr[i] = new bestName();
arr[i].name = employeeArr[i].name;
arr[i].avg = getAvg(employeeMatrix, i);
相关问题