aspose file tools*
The moose likes Beginning Java and the fly likes First Java Assignment, simple problem with code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "First Java Assignment, simple problem with code." Watch "First Java Assignment, simple problem with code." New topic
Author

First Java Assignment, simple problem with code.

J Doyle
Greenhorn

Joined: Dec 12, 2011
Posts: 4
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!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
J Doyle
Greenhorn

Joined: Dec 12, 2011
Posts: 4
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Dec 12, 2011
Posts: 4
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: First Java Assignment, simple problem with code.