Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the number of items in a list

Tags:

java

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.

like image 504
user695696 Avatar asked Apr 24 '11 22:04

user695696


1 Answers

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);
like image 133
xkonnectDeveloper Avatar answered Sep 30 '22 09:09

xkonnectDeveloper