If I wrap this in a try-catch statement the execution goes to the else clause and aborts the loop. The parameter in Eclipse for Level1 is set to "Str1,Str2,Str3,Str4,Str5".
Rob Hunter wrote:I was using ArrayList because of the flexibility to add and remove items from it later on, does List allow the same sort of flexibility?
That's a meaningless question. A List variable can refer to all sorts of objects which implement the List interface. Some classes which implement List allow you to modify the list that way, others don't.
So the question you should ask is, does the result of Arrays.asList() return a list which you can add entries to? And the answer is...
The API documentation wrote:Returns a fixed-size list backed by the specified array.
Are you suggesting that if you create an array with 5 elements, and then later you create another array with 4 elements, then the latter array will actually have 5 elements? It's easy enough to write a program to test that hypothesis:
Wouter Oet wrote:Arrays.asList() doesn't return an ArrayList. It returns an implementation of the List interface. If you really need an ArrayList you can try: new ArrayList<String>(Arrays.asList(...));
By creating a new ArrayList with the return value of Arrays.asList as argument, the ArrayList will contain all the array elements - they are copied into the ArrayList. and because you're explicitly using an ArrayList you can modify it however you like.
I assume you meant the exception clause.
Rob Hunter wrote:went immediately ... to the else clause and the Exception's getMessage returned null.
What's the value of levelCount. It must be >= 0 and <= the current size of the list. The fact that you are decrementing it each time round the loop suggest that it is greater than the current size of the list and so the add() method will throw an IndexOutOfBoundsException.
If you want the first item you add to appear at the end of the list, then just add each item at position zero. This will push all the items in the list down by one place each time.
Edit: See Sean's post below before trying to fix the problems I have pointed out.
Rob Hunter wrote:... the dataList line went immediately (after stopping in the store to get the cookies) to the else clause and the Exception's getMessage returned null.
It is null because you have recieved a NullPointerException, you should printStackTrace() rather than getting the message.
The reason is that you have not initialised the list:
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS