Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to access an array that is part of an object.

I am getting the error "Exception in thread "main" java.lang.NullPointerException at OrderedStringList.add(OrderedStringList.java:21) at Main.main(Main.java:24)"

I have cut my program down to the bare bones, cutting out everything that might be interfering with the output.

public class Main {

public static void main(String[] args) {

    int x = 5;

    OrderedStringList myList = new OrderedStringList();

    myList.add(x);
    }
} //end class

This code references the class OrderedStringList.

public class OrderedStringList {

public int values[];

OrderedStringList(){
    int values[] = new int[5];
}

public void add(int y) {
    values[0] = y;
    System.out.print(values[0]);
}

I assume that the numbers 21 and 24 in the error are line numbers. Because I have some things commented out in my original code, the code I have posted would normally have some content in the middle of it. Line 24 in main is: myList.add(x);. Line 21 of OrderedStringList is: values[0] = y;.

I'm guessing that there is something really simple that I am missing. Anything is appreciated.

Thanks.

share|improve this question
1  
Main.java:24 and OrderedStringList.java:21 do indeed point to the class and line number you're having the error at. –  nhgrif Sep 27 '13 at 0:41
    
In your constructor, int values[] = new int[5]; declares a local variable values! Note, your compiler should have warned you about this unused variable. –  Ken Y-N Sep 27 '13 at 0:59

4 Answers 4

up vote 5 down vote accepted

Here

OrderedStringList(){
    int values[] = new int[5];
}

You shadow the class member values.

Change this to:

OrderedStringList(){
    values = new int[5];
}
share|improve this answer
1  
+1 Beat me by seconds :P –  MadProgrammer Sep 27 '13 at 0:41
    
Thanks a lot! it's been several months since I worked with java and I guess I was rustier than I thought. Greatly Appreciated. –  UnlovedPanda Sep 27 '13 at 3:48

You've declared values twice....

public int values[];

OrderedStringList(){
    int values[] = new int[5];
}

This commonly known as shadowing.

Change the initialization of the array in the constructor to something like...

public int values[];

OrderedStringList(){
    value = new int[5];
}

Instead...

share|improve this answer
1  
+1 for reprocity :-P –  jedwards Sep 27 '13 at 0:41

This declares the values[] array just inside the scope of the method.

OrderedStringList(){
    int values[] = new int[5];
}

If want to refer to the Class scope use

OrderedStringList(){
    values = new int[5];
}
share|improve this answer
    
:-) Sorry, have just seen a lot of unbelievable "answers" tonight –  jedwards Sep 27 '13 at 0:44

With the line int values[] = new int[5];, you're declaring an entirely new int[] that exists only in the constructor. Change it to values = new int[5];.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.