-2

I am getting the error Exception in thread “main” java.lang.NullPointerException?. on my program. What I did was create an array of objects (the array is dbase[], the object is employee). the employee object creates more objects (date, name, and address) that have setter and getter methods. I am getting this error when tryinig to access these object's getter methods when trying to use something such as dbase[0].date.getMonth. I believe this is something wrong with creating the array. I have not included the date or address classes in the interest of saving space, because are basically the same as the name class. thanks for the help.

edit: The error occurred at the second line of the printing block in the main method in the for loop. It occurs at dbase[j].name.getFirst() at line 28

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
    Can you show us what line the nullPointerException pointed to? – mjkaufer Jan 13 '14 at 01:22
  • 2
    Please, edit your post with the error [stack trace](http://en.wikipedia.org/wiki/Stack_trace) – Christian Tapia Jan 13 '14 at 01:24
  • I just added what line the error occurs at – user3188486 Jan 13 '14 at 01:28
  • possible duplicate of [accessing object variables in java](http://stackoverflow.com/questions/21081865/accessing-object-variables-in-java) – Carsten Hoffmann Jan 13 '14 at 01:29
  • This was discussed at http://stackoverflow.com/questions/21081865/accessing-object-variables-in-java already. The OP did not fix what was discussed there. – Carsten Hoffmann Jan 13 '14 at 01:30
  • _Please_ use capital letters for class names and lower case for variable names, like everyone else in the world does. Otherwise your code is much harder to read - it makes it quite tough to tell which identifiers are class names and which are variable names. _Especially_ if you use the same names for both. – Dawood ibn Kareem Jan 13 '14 at 01:48

2 Answers2

1

In your constructor for Employee, you're assigning to local variables, not to the fields of the same name.

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

You can eliminate this problem by removing the type names from the assignment statements:

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

In addition, you should consider beginning all your type names with an uppercase character. This is almost universal Java style, and helps you identify them at a glance.

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

One way to reduce the likelihood of name collisions between member and local variables is to adopt a different naming scheme for the former. This is not generally accepted Java style, and some feel antipathy toward it as an incursion of a style that originated in C++. However, it can reduce risk.

class Employee {
    int m_employeeID = 0;
    Name m_name;
    Address m_address;
    Date m_date;
    ...
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Could you explain a bit further. I'm not exactly sure how to fix this error. – user3188486 Jan 13 '14 at 01:32
  • Your constructor contains declarations like "address address = ....". This declares a local variable of the type "address" and the name "address". But you want to assign to the member field "address." So eliminate the type name, and just say "address = ...". – Andy Thomas Jan 13 '14 at 01:37
0

When you have a variable holding an object in Java, this variable is a reference to the object. That means that multiple variables can point to the same object and also that they might point to no object at all. That's null. When you access something that belongs to an object, either a method or a field, you are dereferencing. However if the variable points to no object (that's null) you get a nullpointer exception.

In your case that could be dbase[j].name or one of the others in the last for-body. And there are two dereferenciations going on: first dbase[j] (I don't see a problem there; actually you are creating multiple objects, i.e. your first for-loop is not needed), or the value of name within your object is not set. Either because Input.getString returned null (we cannot say what that is, because that info is missing from your post) or you didn't set the value in the Employer constructor, which is the case here.

Within the constructor you create local variables.

name name = new name( firstName1, lastName1 );

Instead of

this.name = new name( firstName1, lastName1 );

As a general tip you should look into debuggers (like the one in eclipse or netbeans or whichever ide you use). They can really help you with this kind of problems.

Mene
  • 3,739
  • 21
  • 40