0

I have an ArrayList item that holds Objects

ArrayList<Object> nodeList;

I fill it with records I retrieve form database like this

//start loop
nodeList.add(getResultSet().getString(i).trim());
//end loop

Now, I want to get those results, so I create a "getter" method

public Object getResults(){
    for(int i = 0; i < nodeList.size(); i++){
        return nodeList.get(i);
    }
}

Netbeans tells me that return statement is missing, although I return every single object of the arrayList inside loop. How am I supposed to fix it? I tried to return an Array of Objects but it didn't work either. It's still in a loop an it wants to return something outside loop. Thank you in advance.

7
  • 1
    What if nodeList.size() returns 0? Then the for loop will never run and nothing will be returned so you need a return statement after the for loop Commented Jul 4, 2014 at 19:20
  • 1
    The method will return just the first object and not the others. Commented Jul 4, 2014 at 19:21
  • 1
    If you're only storing Strings in your List, then change the type declaration to List<String> instead. Commented Jul 4, 2014 at 19:26
  • @LuiggiMendoza Actually I want to use it for every type of data -String, int etc. What if I cast that objects? Commented Jul 4, 2014 at 19:31
  • 1
    You should not store the data in a List<Object>, instead create an entity with the desired fields and fill each field in a class instance. Commented Jul 4, 2014 at 19:33

2 Answers 2

4

Netbeans is complaining because, if the list is empty, the code will not enter the for loop body and no value is ever returned.

Also your getter method makes no sense. You are always returning the first element in the list because return will exit the method. If you "want to get those results", you should just return the whole nodeList.

public List<Object> getResults() {
   return nodeList;
}

If you want to get element at index i, you would write the getter as follows:

public Object getResult(int i) {
   return nodeList.get(i);
}

Also, as pointed out by Luiggi in comments, you could avoid adding the results in a list of Objects and instead create an entity class with fields corresponding to the retrieved columns.

Sign up to request clarification or add additional context in comments.

1 Comment

manouti & @Evgeny Makarov thank you so much about your solution. It works now and I get data the way I wanted.
2

Once you get to return command, method finished. You getting error because in case you will provide empty list as method argument, the for loop will be ignored and method should return something in this case.

If you want to return some value from list, you should write something like:

public Object getResults(int position){
        return nodeList.get(position);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.