Results 1 to 8 of 8
  1. #1
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default ArrayList IndexOutOfBounds

    I can't figure out what the problem is. Not sure if this isn't a scanner problem.
    I get an Index out of bounds error when I run this; what really confuses me is I don't print my "here" msg on line 45; don't know why it skips it. Any pointers or advice greatly appreciated.


    Java Code:
    package test;
    
    public class Main {
    
        public static void main(String[] args) {
            Chapter chap = new Chapter();
            CardTemplates temp = new CardTemplates();
            temp = chap.SelectTemplate();
        }
    
    }
    
    package test;
    import java.util.*;
    import java.io.Serializable;
    
    public class Chapter implements Serializable{
    
        ArrayList<CardTemplates> ct;
    
        public Chapter()
        {
            ct = new ArrayList<CardTemplates>();
        }
    
        public CardTemplates SelectTemplate()
        {
            Scanner sc = new Scanner(System.in);
            int x;
            System.out.print("type any letter to create a template  ");
    
                sc.nextLine();
                //create a new template
                String a;
                String b;
                String c;
                String d;
                String e;
                String f;
                System.out.print("Name of template ");
                a=sc.nextLine();
                System.out.print("Front ");
                b=sc.nextLine();
                System.out.print("fwd ");
                c=sc.nextLine();
                System.out.print("Back ");
                d=sc.nextLine();
                System.out.print("bkwd ");
                e=sc.nextLine();
                System.out.print("note ");
                f =sc.nextLine();
                System.out.print("here");
                ct.add(new CardTemplates( a,b,c,d,e,f));
                x=ct.size();
    
            return ct.get(x);
    
        }
    }
    
    
    package test;
    
    import java.util.*;
    import java.io.Serializable;
    
    public class CardTemplates implements Serializable{
    
        private String first;
        private String second;
        private String third;
        private String fourth;
        private String fifth;
        private String templateName;
    
        public CardTemplates()
        {
            first="";
            second="";
            third="";
            fourth="";
            fifth="";
            templateName = "default";
        }
    
        public CardTemplates(String tN, String f1, String f2, String f3, String f4, String f5)
        {
            templateName=tN;
            first = f1;
            second = f2;
            third = f3;
            fourth = f4;
            fifth = f5;
        }
    
        public void setTName(String s)
        { templateName=s;}
    
        public void setFirst(String s)
        { first=s;}
    
    
        public void setSecond(String s)
        { second=s;}
    
        public void setThird(String s)
        { third=s;}
    
        public void setFourth(String s)
        { fourth=s;}
    
        public void setFifth(String s)
        { fifth=s;}
    
        public String getTName()
        { return templateName; }
    
        public String getFirst()
        { return first; }
    
        public String getSecond()
        { return second; }
    
        public String getThird()
        { return third; }
    
        public String getFourth()
        { return fourth; }
    
        public String getFifth()
        { return fifth; }
    
    
    
        public String toString()
        {
            return String.format(first+" "+second+" "+third+" \n"+fourth+" "+fifth);
        }
    
    }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Could we get a stacktrace of the exception, please?

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Although I might've figured it out anyway... I'm fairly certain that the exceptions happens at this line:
    Java Code:
            return ct.get(x);
    Look around that line and see if you can figure it out.

  4. #4
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    But why do I get an array out of bounds error and I'm not using an array at the time?

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Well, you _are_ using an ArrayList, which has an array as the underlying structure.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You are using an ArrayList, like the name suggests it uses an array.

    You set x to be the size of the array an then try to get the item at size. Just like normal arrays if you have 5 items the valid indicies are 0 - 4, making 5 out of bounds.

  7. #7
    graympa is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Thumbs up

    Thanks. You were right the problem was my array index was size, not size-1. I was confused because the error was occurring at the println code, not the array operation following. Maybe it is a pipeline issue.

  8. #8
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    you use ct.get(x-1) it will not get ArrayIndexOutBoundExcepation

Similar Threads

  1. Replies: 3
    Last Post: 04-13-2011, 05:07 PM
  2. Java IndexOutOfBounds Exception: Index 0, Size 0
    By Maricica in forum Threads and Synchronization
    Replies: 7
    Last Post: 03-29-2011, 02:01 PM
  3. need help with IndexOutOfBounds error
    By ShinTec in forum Advanced Java
    Replies: 3
    Last Post: 04-29-2010, 10:21 AM
  4. ArrayList IndexOutOfBounds... error
    By Arius in forum New To Java
    Replies: 7
    Last Post: 01-17-2010, 11:15 PM
  5. Confusing IndexOutOfBounds Error?
    By tibbyuk in forum New To Java
    Replies: 2
    Last Post: 08-09-2008, 12:42 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
  •