-4

My code compiles well, except when displaying the inputted info, i am getting an error i dont understand:

**Exception in thread "main" java.lang.NullPointerException

at Employee.(Employee.java:18)

at unit11.main(unit11.java:50)**

My Code is attached below.

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;
}

}

//and line 50 in the unit11 code( is the employees[i] part)

 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++;
    }
cpjava
  • 1
  • 3

1 Answers1

2

Your constructor is expecting inZip to be an int:

public Employee(String first, String last, 
    int inMont, int inDay, int inYear,
    String inStreet, String inCity, String inState, int inZip)

But it's a String:

String zip = input.nextLine();

Postal codes aren't integers, they're strings. Change the constructor/class/etc. to expect a string.

David
  • 208,112
  • 36
  • 198
  • 279
  • following this i changed the int inZip to String inZip and am coming to another error, which states a similiar thing at the if(errorCheck....) and saying that inZip is an int and i have it as a string how do i fix that – cpjava Dec 31 '15 at 19:44
  • @cpjava: You'd fix that *the exact same way*. Java is a statically typed language. You have to make sure your variable types match wherever you use them. – David Dec 31 '15 at 19:45
  • i get that, but i cant find where the problem is occuring, i tried changing the public int errorCheck and made int inZip to String inZip and then just got more errors – cpjava Dec 31 '15 at 19:48
  • @cpjava: Usually errors come with some indication of the line of code which produces the error... – David Dec 31 '15 at 19:50
  • @cpjava: I can't reach into your computer and read your errors and fix your code for you, no. If you have *specific* errors and *specific* code which produces those errors, feel free to update the question with that information. (Unless it completely changes the nature of the question, in which case asking a new question would be advised.) – David Dec 31 '15 at 20:01
  • thanks for the help i got it to work, but now when i compile, after it ask me th year it gives me a error described as Exception in thread "main" java.lang.NullPointerException at Employee.(Employee.java:18) at unit11.main(unit11.java:50) – cpjava Dec 31 '15 at 20:10
  • @cpjava: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – David Dec 31 '15 at 20:11
  • i do not fully understand what they are trying to say – cpjava Dec 31 '15 at 20:19
  • i still dont get how to fix this – cpjava Dec 31 '15 at 21:10
  • @cpjava: You're going to have to provide actual information about the error. If you're getting a `NullPointerException` then the linked question has canonical and informative answers about that. Either way, nobody here can help you if all you can do is say "my code doesn't work". You have to *show your code* and *explain the problem*. – David Dec 31 '15 at 21:13