• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

First Java Assignment, simple problem with code.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm doing my first java assignment and I'm almost finished, but I keep getting an error in the same place, so I took the part of the code out of the assignment project and tried to run it by itself, but I still can't figure out what's wrong. Here is the code:

I get the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at nodeArray.getElement(nodeArray.java:12)
at Test.main(Test.java:19)





Any help would be appreciated, thanks!
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this line:
i = 2*i + 2;
evaluates to 2 which is beyond the size 1(apparently)
also this line makes no sense:
nodeArray.getElement(i);
you are retrieving the element but doing nothing with it
the real problem seems to be here:
if(nodeArray.getElement(i) == null)
if(nodeArray.getElement(2) == null) when there is no element 2

this line also does nothing:
public int i = 0;
in the whole rest of that class all the i's are local variables passed to the methods.

 
J Doyle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:this line:
i = 2*i + 2;
evaluates to 2 which is beyond the size 1(apparently)
also this line makes no sense:
nodeArray.getElement(i);
you are retrieving the element but doing nothing with it
the real problem seems to be here:
if(nodeArray.getElement(i) == null)
if(nodeArray.getElement(2) == null) when there is no element 2



Thanks for the reply, I realised this a few minutes ago, so I made a new method in the nodeArray class:



And tested the following code:



And it still says that the size of nodeArray is 1.. What am I doing wrong?
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure, sorry. someone else can help i am sure. i do know that now this line
nodeArray.increaseSize(2*i + 2);
equals 6 which is probably not what you intended.

 
J Doyle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:not sure, sorry. someone else can help i am sure. i do know that now this line
nodeArray.increaseSize(2*i + 2);
equals 6 which is probably not what you intended.



Yeah, I just wanted to see if its size increased, it didn't really matter what number I chose. Thanks anyway, I hope someone else knows...
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public ArrayList<String> Array = new ArrayList<String>();

your variable should be all lowercase "array" not "Array"
only class and interface names start with a capital letter
this may have been the whole problem to begin with
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change the name of the ArrayList to "array", or even better maybe "theArray"
i would also change public int i = 0; to public int index = 0; //will clarify things
i think you meant do
this.i = i;
in some of your methods

i like
index = i;
better
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J Doyle wrote:Thanks for the reply, I realised this a few minutes ago, so I made a new method in the nodeArray class:


You need to understand the difference between the size and the capacity of an Arraylist.
The size is the number of elements it currently contains - you can only add a new value to an Arraylist when index >= 0 and index <= size.
The capacity is the size of the array that the ArrayList uses in the background to store values, but it only uses contiguous elements of the array i.e. a value cannot be added to element 4 of the array until values have been added to elements 0 - 3.
 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this error still occuring? If so, I would like to see the complete code again after the changes in order to see if I can hunt down the issue. By the way, did you look up what an IndexOutOfBoundsException is, and how an ArrayList works with the proper java library methods?
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am guessing that J figured it out by now. if not feel free to keep asking. that is why we are here after all.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic