无法使用对象 - java.lang.NullPointerException错误

时间:2015-12-06 07:59:32

标签: java

我正在尝试使用一个使用其他三个类作为数据字段的类。不幸的是,尝试获取嵌套对象的值似乎不起作用。

这是用于测试类的主程序:

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)

是否可以识别,我对类的聚合做错了什么?

3 个答案:

答案 0 :(得分:2)

IN pynotify.init("TEST") notice=pynotify.Notification("Your Score card",string_to_show) notice.show() contructor Employee是局部变量。

name

将其更改为:

Name name = new Name( name1 , name2 );

这样实例变量name = new Name( name1 , name2 );将被初始化

答案 1 :(得分:1)

在此处删除班级名称:

Name name = new Name( name1 , name2 );

因为你没有在这里设置你的类的 name 变量,所以你只是重新创建一个新变量,所以另一个变量仍为空。

所以它应该是:

name = new Name( name1 , name2 );

地址 date 变量存在同样的问题。

答案 2 :(得分:0)

您遇到此错误java.lang.NullPointerException,因为您应该在Name的构造函数中创建Employee的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);