Problem with Getters and Setters of an Array

Asked

Viewed 390 times

0

I’m having trouble accessing arrays in a Singleton class

Class Info

public class Info {
private static Info cinfo;

private String[] first_name;
private String[] last_name;
private String[] nickname;


public static Info getInstance() {
    if (cinfo == null) {
        cinfo = new Info();
    }
    return cinfo;
}

public String getFirst_name(int index) {
    return first_name[index];
}

public void setFirst_name(String first_name , int index) {
    this.first_name[index] = first_name;
}

public String getLast_name(int index) {
    return last_name[index];
}

public void setLast_name(String last_name, int index) {
    this.last_name[index] = last_name;
}

public String getNickname(int index) {
    return nickname[index];
}

public void setNickname(String nickname, int index) {
    this.nickname[index] = nickname;
}

}

Main

public class main {

public static void main(String[] args) {
    Info cinfo = new Info();
    cinfo.setFirst_name("Lucas", 0);
    cinfo.setFirst_name("Bertz", 1);
    System.out.println(cinfo.getFirst_name(0));

}

}

Error

Exception in thread "main" java.lang.NullPointerException
at Info.setFirst_name(Info.java:28)
at main.main(main.java:6)
  • Why are you representing your data this way? Why not use classes? I will answer about the mistake you are having, but I would like to know what the motivation behind this design.

  • By the way, if you’re instantiating Info with new, then your class is not Singleton... the builder of Info should be private, and the first line of its main should be Info cinfo = Info.getInstance();

  • @mgibsonbr This is just a test

1 answer

2


Your error occurs because the array first_name is not instantiated (i.e. it is null). When trying to access it, occurs the NullPointerException.

In Java, arrays have a fixed size, so it is not possible to create a simple array that resizes to receive new elements (for this, the most appropriate is to use java.util.ArrayList). Therefore, you need to choose a default size for your(s) array(s), and already in the constructor (or instance initialization) create an array of this size:

public class Info {
    private static Info cinfo;

    private String[] first_name = new String[10];
    private String[] last_name = new String[10];
    private String[] nickname = new String[10];

This way you will have an array with 10 elements, all null. Your assignment will become successful, provided it is clear that the content is within the acceptable range (0 to 9). That is, it is important to check this in the function call, and react accordingly (throw an exception, replace the array with a larger one, etc).

  • That’s what I was thinking... how does the arraylist work? never used before, you would have an example there?

  • 1

    There are several questions right here at Sopt about ArrayList, search and you will find several examples. Here’s a, including reference to the official documentation (in English). Here’s another one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.