2013-12-13 76 views
0

我寫了這段代碼,除了一件事情之外,一切正常。 在方法whatIsThebestEmployeeName我有問題將字符串從一個對象複製到另一個。它告訴我nullpointerexception。 你並欣賞幫助:)java 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(); 
} 

}

+0

一個NullPointerException異常意味着你提領一空引用之前實例arr[i]。哪行代碼導致異常?這條線上的哪些值被取消了?你是否檢查過這些值是否爲空? – davmac

回答

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

也許你想要的東西,像THID

而且,我只注意到的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()或通過構造函數設置它們的值。您應該查看所有代碼,以確保其正確。

0

有試圖設置其屬性

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