java String array

Posted on 2011-04-25
Last Modified: 2012-05-11

I have the following code:

Outlink[] otherOutLinks;
otherOutLinks = new Outlink[0];
otherOutLinks[0]=new Outlink(link, "Next");

Why does the third line cause an exception?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

How can I fix it?

Question by:wsyy
    LVL 47

    Assisted Solution

    becaues when you say otherOutLinks = new Outlink[0];
    you create array with zero elelemnts
    nothing can go there
    yyou eed at leas
    otherOutLinks = new Outlink[1];
    LVL 47

    Expert Comment

    This could  work:

    Outlink[] otherOutLinks;
    otherOutLinks = new Outlink[1];
    otherOutLinks[0]=new Outlink(link, "Next");

    LVL 92

    Accepted Solution

    Java arrays are zero-indexed, which means the first element is at index zero

    > otherOutLinks = new Outlink[0];

    You create an array with 0 elements

    > otherOutLinks[0]=new Outlink(link, "Next");

    Then you try and set the value of the first element
    this fails because your array only has room for zero elements

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now