我的代码编译得很好,除非显示输入的信息,我收到错误,我不明白:
**线程“main”中的异常java.lang.NullPointerException
在员工处。(Employee.java:18)
at unit11.main(unit11.java:50)**
我的代码附在下面。
public class Employee {
public Name name;
public Date date;
public Address address;
public String[] data;
public Employee(String first, String last,
int inMont, int inDay, int inYear,
String inStreet, String inCity, String inState, String inZip){
if (errorCheck(inMont, inDay, inYear, inState, inZip) == 1)
System.out.println("Error in data. Please try again.");
else{
name = new Name(first, last);
date = new Date(inMont, inDay, inYear);
address = new Address(inStreet, inCity, inState, inZip);
data[0] = name.firstName + " " + name.lastName;
data[1] = date.month + " " + date.day + ", " + date.year;
data[2] = address.street + ", " + address.city + ", " + address.state + ", " + address.zip;
}
}
public int errorCheck(int inMonth, int inDay, int inYear,String inState, String inZip){
if(inMonth < 1 || inMonth > 12 || inYear < 1000 ||
inDay < 1 || inDay > 31 ||
((inMonth == 4 || inMonth == 6 || inMonth == 9 || inMonth == 11) && inDay > 30)
|| (inMonth == 2 && inDay > 29) || inState.length() != 2)
return 1;
else return 0;
}
}
//和unit11代码中的第50行(是雇员[i]部分)
if(errorCheck(month, day, year, state, zip) ==1)
System.out.println("Invalid data input. Please try again.");
else{
employees[i] = new Employee(firstName, lastName, month, day, year, street, city, state, zip);
i++;
}
答案 0 :(得分:2)
您的构造函数期望inZip
成为int
:
public Employee(String first, String last,
int inMont, int inDay, int inYear,
String inStreet, String inCity, String inState, int inZip)
但它是String
:
String zip = input.nextLine();
邮政编码不是整数,它们是字符串。更改构造函数/类/ etc.期待一个字符串。