Friday 15 July 2011

java - return with while and if else loop -


I have the following method that expects some return type as a certain object, the MapData () function returns some objects As soon as a condition is satisfied, I want to come out of the loop. I have not got the compilation error due to any return type. Can you please tell me the issue with my situation?

  Public Fixed Some Object Mapper (list & lt; string & gt; mylist) {Iterator iter = mylist.iterator (); While (iter.hasNext ()) {object [] result = (object []) iter.next (); If (condition) {// dosomething return map data (ABC); } Else if (condition) {// dosomething return mapData (def); } And {// Current data of return data (d); }} // In this situation, the compilation error is not received due to any return type}    

What is imagine when your while loop is never executed? This will happen when your list is empty. In that case, your method does not return anything, thus error occurs.

Just add a return statement:

  return null;   

Return a default example of your while while loop or some objects , if you have < Code> null not checked.

No comments:

Post a Comment