2013-08-29 83 views
1

我對java非常陌生,並且非常接近完成一個項目,我一直試圖完成很長一段時間。每當我嘗試運行代碼。只要構造器被調用,它就會給出一個空指針異常。我的代碼列在下面,任何幫助將不勝感激。使用類構造函數設置數組值時出現NullPointerException

主要類:

import java.util.Scanner; 

public class Main 
{ 
    public static void main(String[] args) 
    { 
     Scanner Input = new Scanner(System.in); 

     System.out.println("Enter the number of employees to register."); 
     int arraySize = Input.nextInt(); 
     Input.nextLine(); 

     Employee employee = new Employee(arraySize); 

     String namesTemp; 
     String streetTemp; 
     String cityTemp; 
     String stateTemp; 
     String zipCodeTemp; 
     String dateOfHireTemp; 

     for(int x = 0; x < arraySize; x++) 
     { 
      System.out.println("Please enter the name of Employee " + (x + 1)); 
      namesTemp = Input.nextLine(); 
      System.out.println("Please enter the street for Employee " + (x + 1)); 
      streetTemp = Input.nextLine(); 
      System.out.println("Please enter the city of Employee " + (x + 1)); 
      cityTemp = Input.nextLine(); 
      System.out.println("Please enter the state of Employee " + (x + 1)); 
      stateTemp = Input.nextLine(); 
      System.out.println("Please enter the zip code of Employee " + (x + 1)); 
      zipCodeTemp = Input.nextLine(); 
      System.out.println("Please enter the date of hire for Employee " + (x + 1)); 
      dateOfHireTemp = Input.nextLine(); 
      employee.addEmployee(x, namesTemp, streetTemp, cityTemp, stateTemp, zipCodeTemp, dateOfHireTemp); 
      System.out.println("The employee ID for employee " + (x + 1) + " is " + (x + 1)); 
     } 

     for(int x = 0; x < arraySize; x++) 
     { 
      String info[] = employee.getEmployeeInfo(x); 

      System.out.println("Employee ID: " + (x + 1)); 
      System.out.println("Name: " + info[0]); 
      System.out.println("Address: " + info[1]); 
      System.out.println("Date of Hire: " + info[2]); 
     } 
    } 
} 

Employee類:

public class Employee 
{ 
    private EmployeeName name; 
    private EmployeeAddress address; 
    private EmployeeDateOfHire hireDate; 

    public Employee(int arraySize) 
    { 

    } 

    public void addEmployee(int x, String name, String street, String city, String state, String zipCode, String hireDate) 
    { 
     this.name.setName(x, name); 
     this.address.setAddress(x, street, city, state, zipCode); 
     this.hireDate.addDateOfHire(x, hireDate); 
    } 

    public String[] getEmployeeInfo(int x) 
    { 
     String info[] = new String[3]; 
     info[0] = name.getName(x); 
     info[1] = address.getAddress(x); 
     info[2] = hireDate.getDateOfHire(x); 
     return info; 
    } 
} 

EDIT--

這裏是我寫我的數據類。它們都以相同的格式書寫。

名稱類:

public class EmployeeName 
{ 
    private String names[]; 

    public void setArray(int x) 
    { 
     String array[] = new String[x]; 
     this.names = array; 
    } 

    public void setName(int x, String name) 
    { 
     this.names[x] = name; 
    } 

    public String getName(int x) 
    { 
     return this.names[x]; 
    } 
} 
+0

請顯示異常的堆棧跟蹤。 – pamphlet

+0

堆棧跟蹤將包含NPE發生的行號。 – keyser

+0

爲什麼你會有一個構造函數接受一個arraySize,然後不做任何事情? –

回答

3

addEmployee()方法

public void addEmployee(int x, String name, String street, String city, String state, String zipCode, String hireDate) 
    this.name.setName(x, name); 
    this.address.setAddress(x, street, city, state, zipCode); 
    this.hireDate.addDateOfHire(x, hireDate); 
} 

你還沒有初始化name或其他領域。默認情況下,實例字段將被初始化爲null。嘗試解除引用null將導致NullPointerException

你應該在你的構造

public Employee(int arraySize) 
{ 
    this.name = new EmployeeName(); 
    this.address = new EmployeeAddress(); 
    this.hireDate = new EmployeeDateOfHire(); 
} 

我不知道這些類看起來像初始化這些字段,例如。

眼看

private String names[]; 

public void setArray(int x) 
{ 
    String array[] = new String[x]; 
    this.names = array; 
} 

public void setName(int x, String name) 
{ 
    this.names[x] = name; 
} 

你叫setName()這也將拋出NullPointerException,因爲你想取消引用names但它是null。您必須首先調用setArray(),但這樣會失敗,因爲如果x爲0,則將創建一個大小爲0的數組,但是會嘗試訪問索引0處的元素,該元素不存在。如果x爲1,則會創建大小爲1的數組,但嘗試訪問索引爲1(第二個元素)的元素,這會拋出IndexOutOfBoundsException

認真地重新考慮你的設計。爲什麼這EmployeeName類很複雜?你爲什麼不擁有一個字符串字段name。或者兩個字段firstNamelastName

+0

我剛剛發佈了一個使用了類結構的編輯。如果你可以再看看他們,將不勝感激。 –

+0

@AlexanderGulea我已經添加了一些解釋。您的設計存在嚴重缺陷。你將不得不重新思考你如何做事。 –

相關問題