NullPointerException when calling a method from a dynamic object array

Bravo

Good day,

Here is my code:

public class ArrayDirectory implements Directory {

private int allocatedSize = 0;
public Entry[] entryDirectory = new Entry[allocatedSize];

@Override
public void addEntry(Entry newEntry) {
    newEntry = findFreeLocation();

    entryDirectory = Arrays.copyOf(entryDirectory,
            entryDirectory.length + 1);

}

private Entry findFreeLocation() {

    Entry returnedEntry = new Entry();

    for (int i = 0; i < entryDirectory.length; i++) {

        if (entryDirectory[i] == null) {
            break;
        }
        returnedEntry = entryDirectory[i];
    }

    return returnedEntry;
}

I've made the size of the entryDirectory dynamic; it increments each time the addEntry method is used. However, when I am trying to call a method of an entry object from the entryDirectory array, a NullPointerException is thrown.

public static void main(String[] args) {

    ArrayDirectory d = new ArrayDirectory();

    d.addEntry(new Entry("Jack", "Jones", 1234));
    d.addEntry(new Entry("Brad", "Jones", 1234));
    d.addEntry(new Entry("Olga", "Jones", 1234));

    System.out.println(d.entryDirectory[0].getInitials());
}

Here is the getInitials() method of the Entry object.

public Entry(String surname, String initials, int extension){
    this.surname = surname;
    this.initials = initials;
    this.extension = extension;
}


public String getInitials() {
    return initials;
}
Andrew Sun

In addition to Philip Voronov's answer, your findFreeLocation method is also implemented incorrectly. Assuming null means an absence of value, the proper implementation should be like this:

private int findFreeLocation() {
  for (int i = 0; i < entryDirectory.length; i++) {
    if (entryDirectory[i] == null) {
        return i
    }
  }
  return -1;
}

You can then use it like this:

public void addEntry(Entry newEntry) {
  int loc = findFreeLocation();
  if (loc >= 0) {
    entryDirectory[loc] = newEntry;
  } else {
    entryDirectory = Arrays.copyOf(entryDirectory, entryDirectory.length + 1);
    entryDirectory[entryDirectory.length - 1] = newEntry;
  }
}

That said, I highly suggest you use a built-in collection, like ArrayList, to handle automatically resizing arrays. They are much easier to use, and their performance is also better (increasing the array size by one means you have to resize every time an item is added, in comparison to ArrayList's implementation, which doubles the size every time it fills up).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NullPointerException when calling a Fragment method from Activity?

From Dev

NullPointerException when calling a method from a different class

From Java

NullPointerException when calling method from another class

From Java

NullPointerException when calling a method reference to an arbitrary object with null argument

From Dev

NullPointerException when calling a mocked method

From Dev

NullPointerException when calling mocked method

From Dev

NullPointerException when calling EJB method

From Dev

NullPointerException when calling Redis method

From Dev

Calling javascript Service/Method from array object

From Dev

SpringBoot @Autowired NullPointerException when calling method from service class

From Dev

coredump when calling virtual method of a dynamic allocated object

From Dev

When Calling Other Activity Method Showing NullPointerException

From Dev

Using TestNG @DataProvider, it returns "[Utils] [ERROR] [Error] java.lang.NullPointerException" when calling a method() that returns Object[][]

From Dev

NullPointerException while calling a method from service

From Dev

Calling method in an element of a object array

From Dev

Calling a method when initializing object

From Dev

Return one value from an array when calling a method

From Dev

Mockito : java.lang.NullPointerException when calling method from mocked Class

From Dev

Calling method of object property in mocked object results in NullPointerException

From Dev

Calling method from member object

From Dev

java NullPointerException when I use "Object A" to call a method from "Object B"

From Dev

NullPointerException when object is initialized in another method

From Dev

calling an object method from another method not working

From Dev

AngularJS syntax error when calling the same method with different params from dynamic button click events

From Java

Getting java.lang.NullPointerException when calling Method.invoke

From Dev

hibernate throws NullPointerException when calling query.list method

From Dev

Java: Calling an object's method from another class when the object is initialized in the initialization block

From Dev

Calling instance method on each object in array

From Dev

(javascript) Calling a method on an object inside an array

Related Related

  1. 1

    NullPointerException when calling a Fragment method from Activity?

  2. 2

    NullPointerException when calling a method from a different class

  3. 3

    NullPointerException when calling method from another class

  4. 4

    NullPointerException when calling a method reference to an arbitrary object with null argument

  5. 5

    NullPointerException when calling a mocked method

  6. 6

    NullPointerException when calling mocked method

  7. 7

    NullPointerException when calling EJB method

  8. 8

    NullPointerException when calling Redis method

  9. 9

    Calling javascript Service/Method from array object

  10. 10

    SpringBoot @Autowired NullPointerException when calling method from service class

  11. 11

    coredump when calling virtual method of a dynamic allocated object

  12. 12

    When Calling Other Activity Method Showing NullPointerException

  13. 13

    Using TestNG @DataProvider, it returns "[Utils] [ERROR] [Error] java.lang.NullPointerException" when calling a method() that returns Object[][]

  14. 14

    NullPointerException while calling a method from service

  15. 15

    Calling method in an element of a object array

  16. 16

    Calling a method when initializing object

  17. 17

    Return one value from an array when calling a method

  18. 18

    Mockito : java.lang.NullPointerException when calling method from mocked Class

  19. 19

    Calling method of object property in mocked object results in NullPointerException

  20. 20

    Calling method from member object

  21. 21

    java NullPointerException when I use "Object A" to call a method from "Object B"

  22. 22

    NullPointerException when object is initialized in another method

  23. 23

    calling an object method from another method not working

  24. 24

    AngularJS syntax error when calling the same method with different params from dynamic button click events

  25. 25

    Getting java.lang.NullPointerException when calling Method.invoke

  26. 26

    hibernate throws NullPointerException when calling query.list method

  27. 27

    Java: Calling an object's method from another class when the object is initialized in the initialization block

  28. 28

    Calling instance method on each object in array

  29. 29

    (javascript) Calling a method on an object inside an array

HotTag

Archive