Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with fixing this error: ArrayIndexOutOfBoundsException: 1

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Need help with fixing this error: ArrayIndexOutOfBoundsException: 1

    Here is my code for this method:
    (for passed-in desc, it has format as "brand: model"
    For example, if the brand is Whirlpool and the model is LX1279, the description will be Whirlpool:LX1279. If only the brand is set, the description should contain the brand followed by a colon. If only the model is set, the description should contain a colon followed by the model. If both the brand and the model are null, that is equivalent to a null description.)

    public void setDescription(String desc)
    {
    if (desc == null)
    {
    throw new IllegalArgumentException
    ("description is null.");
    }
    else if (!desc.contains(":"))
    {
    throw new IllegalArgumentException
    ("description does not contain a colon.");
    }
    else if (desc.contains(":"))
    {
    String[] tok = new String[2];
    tok = desc.split(":");

    if (tok[0] != null && tok[1] != null)
    {
    appliance.setBrand(tok[0]);
    appliance.setModel(tok[1]);
    }


    }
    }
    The error suggested the line that contains such problem is at line 53, which I highlighted with bold red color.

    symptom: java.lang.ArrayIndexOutOfBoundsException: 1
    at ApplianceAdapter.setDescription(ApplianceAdapter.j ava:53)

    I am not really familiar with such error, but any hint/explanation/examples will be helpful.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with fixing this error: ArrayIndexOutOfBoundsException: 1

    symptom: java.lang.ArrayIndexOutOfBoundsException: 1
    at ApplianceAdapter.setDescription(ApplianceAdapter.j ava:53)
    At line 53 the code used an index whose value was past the end of the array.
    The array has less than 2 elements and index 1 is invalid
    Remember array indexes range from 0 to the array's length-1;

    Make sure that the array is long enough by using its .length attribute before trying to index into it.

    The split() method creates an array depending on the String it is working on. The value of the array variable will be replaced by what is returned by the split() method.

    Use this for debugging to see the contents of an array:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    mikuya707 (April 13th, 2013)

Similar Threads

  1. Need help fixing
    By brown2292 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2012, 12:53 PM
  2. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 06:52 PM
  3. Fixing the scan input
    By Twoacross in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 14th, 2011, 07:42 AM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM