Solved

array out of bound exception error

Posted on 2006-07-22
4
856 Views
Last Modified: 2012-08-13

Hello there,

i get this error java.lang.ArrayIndexOutOfBoundsException: 0, when i try to do this.
String cmbList[] =new String[0];
System.out.println("Inside ELSE"+cmbList[0]);

what is the problem.

cheers
zolf
0
Comment
Question by:zolf
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
maclema earned 125 total points
Comment Utility
String cmbList[] =new String[1];

when creating the array it is 1 based, so what you are doing is creating an empty array with no elements
0
 

Author Comment

by:zolf
Comment Utility

could you please explain in more detail why i was getting the error.please.array start with 0, am i right
0
 
LVL 5

Expert Comment

by:maclema
Comment Utility
ArrayIndexOutOfBounds means that you are trying to access an array elements that does not exist:

Picturs this.

String arr[] = new String[3];

this would look something like this

["", "", ""]

So when you do

String srr[] = new String[0];

you get this:

[]  << see no elements


When access array elements it is 0 based, 0 is the first element in the array, but if you have no elements in the array, there is nothing to access!
0
 
LVL 5

Expert Comment

by:maclema
Comment Utility
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now