Skip to content

NullPointerException #130578

Discussion options

You must be logged in to vote

I guess it occurs because you're trying to call toUpperCase() on an element in the data array that might be null. If you haven't initialized an element at the specified index, it will be null by default. You need to check if the element is null before calling toUpperCase().

public class Example {
    private String[] data;

    public Example(int size) {
        data = new String[size];
    }

    public void addData(int index, String value) {
        data[index] = value;
    }

    public String getData(int index) {
        if (data[index] == null) {
            return null;
        }
        return data[index].toUpperCase();
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gw2366
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Programming Help Programming languages, open source, and software development.
2 participants