Results 1 to 8 of 8
Thread: LinkedList errors
- 11-06-2011, 07:23 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
LinkedList errors
Hi everyone, I'm doing a school assignment for Android development and one of the questions was to use a LinkedList instead of a Hashmap for some code that was provided. I'm getting very irritated because whenever I add a new object to the list I get an error. Here is the code
After running I get the error,Java Code:private void loadQuestionBatch(int startQuestionNumber) throws XmlPullParserException, IOException { linkedlistQuestions.clear(); XmlResourceParser questionBatch; questionBatch = getResources().getXml(R.xml.samplequestions); //Android development stuff int eventType = -1; while (eventType != XmlResourceParser.END_DOCUMENT) { if (eventType == XmlResourceParser.START_TAG) { String strtag = questionBatch.getName(); if (strtag.equals(XML_TAG_QUESTION)) { String questionNumber = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_NUMBER); Integer questionNum = new Integer(questionNumber); String questionText = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_TEXT); String questionImageUrl = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_IMAGEURL); linkedlistQuestions.add(questionNum, new Question(questionNum, questionText, questionImageUrl)); } } eventType = questionBatch.next(); } }
java.lang.IndexOutOfBoundsException at java.util.LinkedList.set(LinkedList.java:690)
I'm getting very irritated. I don't know LinkedLists that well but from what I understand they don't have a defined size so why would I be getting an outofbounds error?
- 11-06-2011, 09:16 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: LinkedList errors
Yes and no. They don't have a definite size so you can call add(E) or addAll(Collection) etc and the elements just keep getting added.I don't know LinkedLists that well but from what I understand they don't have a defined size so why would I be getting an outofbounds error?
But you are using the two argument form of add(int,E) which adds an element at a specific place within the list. From those docs it, "Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())". Is there some reason why you are adding at a specific location? If not just append the questions with the other form of add().
-----
It's a good idea to post the entire stack trace that arises with runtime errors. And also (imho) to state the obvious like the type that linkedlistQuestions is declared to be. It just saves guesswork.
- 11-06-2011, 07:28 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Re: LinkedList errors
I need it to be at a certain position because each object must correspond to a certain position.
I just really want to know why it is not allowing me to place at a position. I know that the location I'm trying to put it at is not negative so it must be that the location is bigger than the linkedlist size. How do I increase the size?
The error is in the android dev debug window so it might be a little weird but here it is
11-05 22:59:08.908: ERROR/mytag(29085): java.lang.IndexOutOfBoundsException
11-05 22:59:08.908: ERROR/mytag(29085): at java.util.LinkedList.set(LinkedList.java:690)
11-05 22:59:08.908: ERROR/mytag(29085): at com.androidbook.triviaquiz.QuizGameActivity.loadQu estionBatch(QuizGameActivity.java:283)
11-05 22:59:08.908: ERROR/mytag(29085): at com.androidbook.triviaquiz.QuizGameActivity.onCrea te(QuizGameActivity.java:80)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2466)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2519)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.ActivityThread.access$2200(ActivityThr ead.java:123)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1870)
11-05 22:59:08.908: ERROR/mytag(29085): at android.os.Handler.dispatchMessage(Handler.java:99 )
11-05 22:59:08.908: ERROR/mytag(29085): at android.os.Looper.loop(Looper.java:123)
11-05 22:59:08.908: ERROR/mytag(29085): at android.app.ActivityThread.main(ActivityThread.jav a:4370)
11-05 22:59:08.908: ERROR/mytag(29085): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 22:59:08.908: ERROR/mytag(29085): at java.lang.reflect.Method.invoke(Method.java:521)
11-05 22:59:08.908: ERROR/mytag(29085): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
11-05 22:59:08.908: ERROR/mytag(29085): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
11-05 22:59:08.908: ERROR/mytag(29085): at dalvik.system.NativeStart.main(Native Method)
- 11-06-2011, 10:54 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: LinkedList errors
I'm no expert, but that says "Map" to me. (ie a map from key/position to value/question content).I need it to be at a certain position because each object must correspond to a certain position.
Of course, as you say, the assignment asks you to use a list rather than a map... (If I were doing the assignment, I'd be confused enough to ask whoever assigned it, "why a list for something that's mapping positions to questions"?)
As the docs say, because the position lies outside the list's current size.I just really want to know why it is not allowing me to place at a position.
It might be worth verifying that. Ie printing (or toasting) both position and the list's size().I know that the location I'm trying to put it at is not negative so it must be that the location is bigger than the linkedlist size.
This is the heart of the matter. LinkedList (unlike, say, a JavaScript array or the olde fashioned Vector class) will not happily add null entries to make the array as big as you require at some point. Rather you have to do this yourself.How do I increase the size?
Check the API docs to make sure there is no method already there to do this. (As far as I know there isn't). Then write your own:
Basically your code will append nulls to the list until its size is at least as large as the int you pass.Java Code:/** * Makes sure a given list is at least a given size. */ private void ensureSize(List list, int size) { // your code here }
[Edit] What you end up with is a list that may well have "holes" in it: ie null values. Other code that uses the list had better be prepared to cope with that.
- 11-06-2011, 11:45 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Re: LinkedList errors
I did what you said, I verified why it was out of bounds to find that the list was size 0.
My issue is now trying to increase the size of the list to 15.
I do see that it goes through this message as the system prints "initialized"Java Code:private void initlist(LinkedList <Question>list){ //Question is a class I created System.out.println("Initialized"); int i = 1; while(i < 15){ list.add(null); i++; } }
- 11-07-2011, 12:01 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: LinkedList errors
Are you still getting the index out of bounds exception?
That code looks good for increasing the array size. So if you *are* still getting the exception, check the value of size() and each value of questionNum within loadQuestionBatch() - to check that you have increased the size of the correct list, and that there is nothing in the data with a question number > 14.
Edit: NB, 14. If you try and add question at position 15 there will be trouble.
- 11-07-2011, 12:19 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Re: LinkedList errors
the system print still says the size of the linkedlist is 0 and still gives me the out of bounds exception
- 11-07-2011, 12:40 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: LinkedList errors
There are two possibilities:the system print still says the size of the linkedlist is 0
(1) The list initialisation did not happen as you (and I) thought. This is easy to check for:
(2) The list you initialised was not the list to which you are adding questions:Java Code:/** Ensures that a given list has 14 elements. */ private void initlist(LinkedList <Question>list){ int i = 1; while(i < 15){ list.add(null); i++; } System.out.printf("%s initialised with size %d%n", list, list.size()); }
If it turns out that the problem is (2), then you have to start looking at all the places that the value of linkedlistQuestions is altered.Java Code:/** ??? */ private void loadQuestionBatch(int startQuestionNumber) throws XmlPullParserException, IOException { System.out.printf( "At loadQuestionBatch(), linkedlistQuestions=%s (size=%d)%n", linkedlistQuestions, linkedlistQuestions.size()); // ... etc }Last edited by pbrockway2; 11-07-2011 at 12:45 AM.
Similar Threads
-
Need help with LinkedList.
By fsuarjun03 in forum New To JavaReplies: 0Last Post: 04-18-2011, 07:27 PM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
LinkedList
By [RaIdEn] in forum New To JavaReplies: 7Last Post: 10-13-2009, 12:59 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks