santres
santres

Reputation: 37

Why can't I set an attribute to an object's array?

I want to set a[1] name to Gabriel for example, and it always give me the error:

Exception in thread "main" java.lang.NullPointerException 
at Register.main(Register.java:4)

My code is:

public class Register {
    public static void main(String[] args) {
        Employee a[] = new Employee[2];
        a[0].setName("Douglas");
    }
}

Upvotes: 1

Views: 155

Answers (4)

Emre Demirkol
Emre Demirkol

Reputation: 91

We can assume that your Employee class is as below:

class Employee {
    private String name;

    public Employee(String s) {
        name = s;
    }

    public void setName(String s) {
        name = s;
    }

    public String getName() {
        return name;
    }
}
public class Main {
    public static void main(String[] args) {
        Employee a[] = null;

        if (a == null)
            System.out.println("Employee array is null !!!");

        a = new Employee[10]; // You are init your aray

        a[0] = new Employee("Test Name 1"); // it's init first item of array
        a[1] = new Employee("Test Name 2"); // it's init second item of array
        a[2] = new Employee("Test Name 3"); // it's init third item of array

        for (Employee employee : a) {
            if (employee == null) {
                System.out.println("Employee  is null !!!");
            } else
                System.out.println("Employee  is not null : "
                        + employee.getName());
        }
    }
}

As you see in your code "Employee a[] = new Employee[2];" only allocate space for count of employee. But it is set with null. Because it keeps your null employee references, so you should init them one by one. Because of this reason "a[0].setName("Douglas");" trying to reach null address and throws exception.

If run it you can see this output:

Employee array is null !!!
Employee  is not null : Test Name 1
Employee  is not null : Test Name 2
Employee  is not null : Test Name 3
Employee  is null !!!
Employee  is null !!!
Employee  is null !!!
Employee  is null !!!
Employee  is null !!!
Employee  is null !!!
Employee  is null !!!

Upvotes: 3

user14940971
user14940971

Reputation:

You should add a constructor to the Employee class. Then you can create an array as follows:

public static void main(String... args) {
    Employee[] arr = {
            new Employee("Douglas"),
            new Employee("Douglas2")};

    System.out.println(Arrays.toString(arr));
    // [Douglas, Douglas2]
}
public static class Employee {
    String name;

    public Employee(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

Upvotes: 1

You can use the builder pattern through lombok, and the list can be initialized with non-null objects.

import lombok.Builder;
import org.assertj.core.util.Arrays;

import java.util.List;

public static class Main {

    @Builder
    public class Employee {
        private String name;
    }

    public static void main(String[] args) {
        List<Main.Employee> employees = List.of(
                Employee.builder().name("Douglas").build(),
                Employee.builder().name("Maik").build(),
                Employee.builder().name("Alex").build()
        );
    }
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 311163

When you create an array of non-primitive, you create an array of nulls of the specified size. Before accessing the objects' properties, you need to create them (using new):

Employee a[] = new Employee[2];
a[0] = new Employee(); // Here!
a[0].setName("Douglas");

Upvotes: 4

Related Questions