Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I have a method in Java that returns a List, and it can return null:

Java
public List<Player> route(String message, double[] destination)
{
  List<Player> tempList = new ArrayList<Player>();

 if (.....)
   return null;
 else
  {
     tempList.add(p1);
     tempList.add(p2);
  }

return tempList
}


Now I want to add it to my ArrayList, but it doesn't work.
I do it like this:

Java
myList.addall(route(........);


but it doesn't work. (probably because I return null sometimes).

What do I do wrong?
How should I do it correctly?

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 22-Apr-12 19:31pm    
What is really written in this line with "......". It would not compile... must be "addAll", for example. :-)
Do the actual types of a generic parameter match?
--SA

1 solution

return an empty ArrayList:

Java
if(condition){
  return new ArrayList<player>();
}
</player>


That is much easier to deal with on the receiving side.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900