2015-12-06 58 views
-1

我正在嘗試利用一個類,它使用三個其他類作爲數據字段。不幸的是,嘗試獲取嵌套對象的值似乎不起作用。不能使用對象 - java.lang.NullPointerException錯誤

這是用於測試的類主程序:

public class EmployeeTest 
{ 
public static void main(String [] args) 
    { 
    String name1 = "John"; 
    String name2 = "Smith"; 
    String street = "12 Street"; 
    String state = "MA"; 
    String city = "Boston"; 
    String zip = "50312"; 
    int day = 12; 
    int month = 12; 
    int year = 2001; 
    Employee employee1 = new Employee(name1, name2, street, city, state, zip, month, day, year); 

    System.out.println("Employee's name is " + employee1.GetName1()); 
    } 
} 

這是創建的類的對象:

public class Employee 
{ 
private Name name; 
private Address address; 
private Date date; 
// define data fields 

public Employee(String name1, String name2, String street, String city, String state, String zip , int month, int day, int year) 
    { 
    Name name = new Name(name1 , name2); 
    Address address = new Address(street, city, state, zip); 
    Date date = new Date(); 
    date.setDate(month , day, year); 
    } 
// constructor 

public String GetName1() 
    { 
    String name1 = name.GetFirstName(); 
    return name1; 
    } 
// first name getter 

public String GetName2() 
    { 
    String name2 = name.GetLastName(); 
    return name2; 
    } 

public String GetStreetName() 
    { 
    String street = address.GetStreet(); 
    return street; 
    } 
// street getter 

public String GetCityName() 
    { 
    String city = address.GetCity(); 
    return city; 
    } 
// street getter 

public String GetStateName() 
    { 
    String state = address.GetState(); 
    return state; 
    } 
// street getter 

public String GetZipCode() 
    { 
    String zip = address.GetZip(); 
    return zip; 
    } 
// street getter 

public String GetDate() 
    { 
    String time = date.getDateString(); 
    return time; 
    } 
} 

這是其可以是空的源極的類例外:

public class Name 
{ 
private String FirstName; 
private String LastName; 
// define data fields 

public Name(String name1 , String name2) 
    { 
    FirstName = name1; 
    LastName = name2; 
    } 
// constructor 

public String GetFirstName() 
    { 
    return FirstName; 
    } 
// get first name 

public String GetLastName() 
    { 
    return LastName; 
    } 
// last name getter 

public void SetFirstName(String name1) 
    { 
    FirstName = name1; 
    } 
// first name setter 

public void SetLastName(String name2) 
    { 
    LastName = name2; 
    } 
// last name setter 
} 

不可避免地,使用Employee類似乎產生了這個錯誤:

Exception in thread "main" java.lang.NullPointerException 
at Employee.GetName1(Employee.java:22) 
at EmployeeTest.main(EmployeeTest.java:16) 

可以確定,我在做什麼錯誤的聚合類?

+0

這是爲什麼調試程序時,人們並不認爲使用他們的調試器?這將是微不足道的檢查和修復方式。 –

回答

2

IN Employee構造函數name是局部變量。

Name name = new Name(name1 , name2);

將其更改爲:

name = new Name(name1 , name2);

使實例變量name將被初始化

1

刪除的類名稱的位置:

Name name = new Name(name1 , name2); 

因爲你沒有在這裏設置名字你的類的變量,你只是重新創建一個新的,所以另一個保持爲空。

所以應該閱讀:存在地址日期變量

name = new Name(name1 , name2); 

同樣的問題。

0

你有這樣的錯誤java.lang.NullPointerException因爲你應該在Employee構造器,如創建的Name的istance:

public Employee(String name1, String name2, String street, String city, 
     String state, String zip, int month, int day, int year) { 
    name = new Name(name1, name2); 

不:

public Employee(String name1, String name2, String street, String city, 
     String state, String zip, int month, int day, int year) { 
    Name name = new Name(name1, name2); 
相關問題