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.
Main.java:24
andOrderedStringList.java:21
do indeed point to the class and line number you're having the error at. – nhgrif Sep 27 '13 at 0:41int values[] = new int[5];
declares a local variablevalues
! Note, your compiler should have warned you about this unused variable. – Ken Y-N Sep 27 '13 at 0:59