Results 1 to 8 of 8
Thread: ArrayList IndexOutOfBounds
- 05-12-2011, 11:32 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
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); } }
- 05-13-2011, 12:27 AM #2
Could we get a stacktrace of the exception, please?
- 05-13-2011, 12:33 AM #3
Although I might've figured it out anyway... I'm fairly certain that the exceptions happens at this line:
Look around that line and see if you can figure it out.Java Code:return ct.get(x);
- 05-13-2011, 01:55 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
But why do I get an array out of bounds error and I'm not using an array at the time?
- 05-13-2011, 02:00 AM #5
Well, you _are_ using an ArrayList, which has an array as the underlying structure.
- 05-13-2011, 02:02 AM #6
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.
- 05-13-2011, 05:39 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
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.
- 05-13-2011, 09:04 AM #8
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Why do I get this IndexOutOfBounds Exception at JTable?
By ozzyman in forum New To JavaReplies: 3Last Post: 04-13-2011, 04:07 PM -
Java IndexOutOfBounds Exception: Index 0, Size 0
By Maricica in forum Threads and SynchronizationReplies: 7Last Post: 03-29-2011, 01:01 PM -
need help with IndexOutOfBounds error
By ShinTec in forum Advanced JavaReplies: 3Last Post: 04-29-2010, 09:21 AM -
ArrayList IndexOutOfBounds... error
By Arius in forum New To JavaReplies: 7Last Post: 01-17-2010, 10:15 PM -
Confusing IndexOutOfBounds Error?
By tibbyuk in forum New To JavaReplies: 2Last Post: 08-08-2008, 11:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks