I have a method called public int getSize()
and it is suppose to return the number of items in the list. the instance variable is private Listable[] items;
I thought it would just be something like this:
int size = 0;
for (int i = 0; i < items.length; i++){
size++;
}
return size;
But when I run it through these tests I get this nullpointerexception on the for (int i = 0; i < items.length; i++){
line
I don't think it likes items.length
for some reason. I'm not getting any errors in Java. How should I do this?
i already tried return items.length;
that didnt work either.
http://www.easywayserver.com/blog/java-how-to-get-size-of-list/
I saw this article when I was browsing the web it contains the code that implements the list.size() method.
List<String> ls=new ArrayList<String>();
ls.add("one");
ls.add("Three");
ls.add("two");
ls.add("four");
int sizeOfList=ls.size();
System.out.println("Size of List :"+sizeOfList);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With