Results 1 to 8 of 8
  1. #1
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default 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

    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();
            }
        }
    After running I get the error,


    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?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: LinkedList errors

    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?
    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.

    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.

  3. #3
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default 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)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: LinkedList errors

    I need it to be at a certain position because each object must correspond to a certain position.
    I'm no expert, but that says "Map" to me. (ie a map from key/position to value/question content).

    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"?)

    I just really want to know why it is not allowing me to place at a position.
    As the docs say, because the position lies outside the list's current 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.
    It might be worth verifying that. Ie printing (or toasting) both position and the list's size().

    How do I increase the 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.

    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:

    Java Code:
        /**
         * Makes sure a given list is at least a given size.
        */
    private void ensureSize(List list, int size) {
        // your code here
    }
    Basically your code will append nulls to the list until its size is at least as large as the int you pass.

    [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.

  5. #5
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default 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.

    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++;
        	}
        }
    I do see that it goes through this message as the system prints "initialized"

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  7. #7
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: LinkedList errors

    the system print still says the size of the linkedlist is 0 and still gives me the out of bounds exception

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: LinkedList errors

    the system print still says the size of the linkedlist is 0
    There are two possibilities:

    (1) The list initialisation did not happen as you (and I) thought. This is easy to check for:

    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());
    }
    (2) The list you initialised was not the list to which you are adding questions:

    Java Code:
        /** ??? */
    private void loadQuestionBatch(int startQuestionNumber) 
            throws XmlPullParserException, IOException {
        System.out.printf(
                "At loadQuestionBatch(), linkedlistQuestions=%s (size=%d)%n", 
                linkedlistQuestions, linkedlistQuestions.size());
        // ... etc
    }
    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.
    Last edited by pbrockway2; 11-07-2011 at 01:45 AM.

Similar Threads

  1. Need help with LinkedList.
    By fsuarjun03 in forum New To Java
    Replies: 0
    Last Post: 04-18-2011, 08:27 PM
  2. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  3. LinkedList
    By [RaIdEn] in forum New To Java
    Replies: 7
    Last Post: 10-13-2009, 01:59 AM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •