对象数组java中的空对象

时间:2014-01-13 01:19:33

标签: java

我在线程“main”java.lang.NullPointerException?中收到错误异常。在我的节目上。我所做的是创建一个对象数组(数组是dbase [],对象是employee)。 employee对象创建更多具有setter和getter方法的对象(日期,名称和地址)。当tryinig在尝试使用诸如dbase [0] .date.getMonth之类的东西时访问这些对象的getter方法时,我收到此错误。我认为这是创建数组的错误。为了节省空间,我没有包含日期​​或地址类,因为它们与名称类基本相同。谢谢你的帮助。

编辑:错误发生在for循环中main方法中打印块的第二行。它出现在第28行的dbase [j] .name.getFirst()

public class test {
public static void main (String[] args) {
    int i = Input.getInt ("How many employees would you like to enter? ");
    int j;
    Employee [] dbase = new Employee [i];
    for (j = 0; j < i; j++) {
    dbase[j] = new Employee();
    }
    for (j = 0; j < i; j++) {
        String firstName = Input.getString ("What is an employee " + (j + 1) + "'s first name?");
        String lastName = Input.getString ("What is this employee's last name?");

        String street = Input.getString ("On what street does this employee live?");
        String city = Input.getString ("In which city does this employee live?");
        String state = Input.getString ("In which state does this employee live? (abbreviation)");
        String zipcode = Input.getString ("What is this employee's zip code?");

        int month = Input.getInt ("In what month was this employee born? (numeric - January = 1 )");
        int day = Input.getInt ("On what day was this employee born?");
        int year = Input.getInt ("In what year was this employee born?");

        int employeeID = Input.getInt ("What should this employee's employee id be?");

        dbase[j] = new Employee(firstName, lastName, street, city, state, zipcode, month, day, year, employeeID);
    }
    for (j = 0; j < i; j++) {
        System.out.print ( "Employee number " + (j + 1) + " is named ");
        System.out.print ( dbase[j].name.getFirst() + " " + dbase[j].name.getLast() + " and lives on " + dbase[j].address.getStreet());
        System.out.print ( " in " + dbase[j].address.getCity() + " " + dbase[j].address.getState() + ", " + dbase[j].address.getZipcode());
        System.out.print ( ". He will be hired on " + dbase[j].date.getMonth() + "-" + dbase[j].date.getDay() + "-" + dbase[j].date.getYear() );
        System.out.print ( " and his ID is " + dbase[j].getEmployeeID());
        System.out.println ();
    }
}

}

class Employee {
int employeeID = 0;
name name;
address address;
date date;
Employee(){
}
Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
    name name = new name( firstName1, lastName1 );
    address address = new address( street1, city1, state1, zipcode1 );
    date date = new date( month1, day1, year1);
    employeeID = employeeID1;   
}
int getEmployeeID() {
    return employeeID;
}

}

class name {
String firstName = " ";
String lastName = " ";
name(String newFirstName, String newLastName) {
    firstName = newFirstName;
    lastName = newLastName;
}
String getFirst() {
    return firstName;
}
String getLast() {
    return lastName;
}   

}

2 个答案:

答案 0 :(得分:1)

在Employee的构造函数中,您将分配给局部变量,而不是分配给同名字段。

Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
    name name = new name( firstName1, lastName1 );
    address address = new address( street1, city1, state1, zipcode1 );
    date date = new date( month1, day1, year1);
    employeeID = employeeID1;   
} 

您可以通过从赋值语句中删除类型名称来消除此问题:

Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
    name = new name( firstName1, lastName1 );
    address = new address( street1, city1, state1, zipcode1 );
    date = new date( month1, day1, year1);
    employeeID = employeeID1;   
} 

此外,您应该考虑使用大写字符开始所有类型名称。这几乎是通用的Java风格,可以帮助您一目了然地识别它们。

class Name { ... }
class Date { ... }
class Address { ... }

降低成员和局部变量之间名称冲突可能性的一种方法是对前者采用不同的命名方案。这并不是普遍接受的Java风格,有些人对它的反感是对C ++风格的侵犯。但是,它可以降低风险。

class Employee {
    int m_employeeID = 0;
    Name m_name;
    Address m_address;
    Date m_date;
    ...
}

答案 1 :(得分:0)

如果在Java中有一个包含对象的变量,则此变量是对该对象的引用。这意味着多个变量可以指向同一个对象,也可以指向任何对象。那是null。 当您访问属于某个对象(方法或字段)的内容时,您将取消引用。但是,如果变量指向无对象(即null),则会得到nullpointer异常。

在您的情况下,可能是dbase[j].name或上一个for - 身体中的其他人之一。并且有两个dereferenciations正在进行:第一个dbase[j](我没有看到问题;实际上你正在创建多个对象,即不需要你的第一个for循环),或{{1}的值你的对象内没有设置。要么因为name返回Input.getString(我们不能说那是什么,因为你的帖子中缺少这些信息),要么你没有在Employer构造函数中设置值,这就是这里的情况。 / p>

在构造函数中,您可以创建局部变量。

null

而不是

name name = new name( firstName1, lastName1 );

作为一般提示,您应该查看调试器(例如eclipse或netbeans中的调试器或您使用的任何ide)。他们可以真正帮助你解决这类问题。