2013-12-13 6 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(); 
} 

} 당신이 시도하고 데이터를 변경하기 전에 당신은 배열에 new bestNames를 추가 할 필요가

+0

속성을 설정하기 전에 arr[i]를 인스턴스화해야합니다. 예외의 원인이되는 코드 줄은 무엇입니까? 해당 값에서 참조 해제되는 값은 무엇입니까? 그 값들이 null이 아닌지 확인 했습니까? – davmac

답변

0

을 appriciate.

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

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

코드의 첫 번째 라인은 모든 nullnestName 유형의 어레이를 생성한다. arr[i].namearry[i].avg을 변경하기 전에 모든 인덱스를 new bestName으로 초기화해야합니다.

은 아마 당신은

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

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

는 NullPointerException이 당신이 null 참조를 역 참조 의미

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