java.lang.ArrayIndexOutOfBoundsException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    java.lang.ArrayIndexOutOfBoundsException

    i get
    Exception in thread "main" java.lang.Array IndexOutOfBound sException: 2
    at Producent.main( producent.java: 605)

    when i run it from bat

    @start "Supply Chain Management-Producer to Queue" run Producent queue1 queue2 queue3 queue4

    maybe you will se something i dont see

    [code=java]
    public static void main(String[] args){



    if (args.length < 1 || args.length > 4) {
    System.out.prin tln("usage: <destination> <destination> <destination> <destination>") ;
    System.exit(1);
    }else {Producent producent=new Producent(args[0],args[1],args[2],args[3]);}
    /// producent.close ();

    }
    }
    [/code]

    thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by oll3i
    i get
    Exception in thread "main" java.lang.Array IndexOutOfBound sException: 2
    at Producent.main( producent.java: 605)

    when i run it from bat

    @start "Supply Chain Management-Producer to Queue" run Producent queue1 queue2 queue3 queue4

    maybe you will se something i dont see

    [code=java]
    public static void main(String[] args){



    if (args.length < 1 || args.length > 4) {
    System.out.prin tln("usage: <destination> <destination> <destination> <destination>") ;
    System.exit(1);
    }else {Producent producent=new Producent(args[0],args[1],args[2],args[3]);}
    /// producent.close ();

    }
    }
    [/code]

    thank you
    The error will occur if args.length is 1, 2, or 3. Change your if condition.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      i changed it to
      if (args.length < 1 || args.length > 3)but stiil get the exception

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by oll3i
        i changed it to
        if (args.length < 1 || args.length > 3)but stiil get the exception
        You are complicating things for your self. Don't you just want to run this if you have at least 4 objects in your array.

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by oll3i
          i changed it to
          if (args.length < 1 || args.length > 3)but stiil get the exception
          Your else statement still tries to call args[0], 1, 2, and 3.

          Just think about the following scenarios that go to your else statement:
          Code:
          args.length == 1, it still tries to call args[1],2,3
          
          args.length == 2, it still tries to call args[2],3
          
          args.length == 3, it still tries to call args[3]
          
          only with args.length == 4 or more is it able to call all of these...
          You need to add else if's for each of these cases or do only when args.length > 3...

          Hope this helped you out,

          -blazed

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            .... i threw out the if
            left only
            [code=java]
            public static void main(String[] args){




            Producent producent=new Producent(args[0],args[1],args[2],args[3]);



            }
            }
            [/code]
            and i stil get the exception

            thank you

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by oll3i
              .... i threw out the if
              left only
              [code=java]
              public static void main(String[] args){




              Producent producent=new Producent(args[0],args[1],args[2],args[3]);



              }
              }
              [/code]
              and i stil get the exception

              thank you
              Don't throw away the if. You need it to handle abnormal data.
              Use the if(args.length > 3) that has been suggested.

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                but why it still throws the exception when i got rid of args.length ?
                the exception is in thread main so i shouldn't look somewhere else for it ?

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #9
                  Originally posted by oll3i
                  but why it still throws the exception when i got rid of args.length ?
                  the exception is in thread main so i shouldn't look somewhere else for it ?
                  Dude, refer to my above post. It's hard to be any more clear.

                  It doesn't have ANYTHING to do with trying to access args.length. IndexArrayOutOf Bounds Exception occurs because you're trying to access an index in the array that is too high or too low (doesn't exist) or could not exist (Out Of Bounds, get it? ) .

                  Listen, it's not because of args.length, args.length will always just give you the length of the array, like 1, or 2 or 3 or whatever number.

                  It has to do with the following line of code:
                  Code:
                  Producent(args[0],args[1],args[2],args[3]);
                  If you have no if statement it's even worse, what if args.length == 0? What if no args are inputted? Listen, do one of the following things:

                  Change your if condition to be (args.length > 3) or

                  Make an else if (args.length == 1), one for (args.length == 2) and one for args.length == 3 where you do different things in each.

                  Now are you understanding where the problem lies?

                  I hope this helped, good luck,

                  -blazed

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by oll3i
                    but why it still throws the exception when i got rid of args.length ?
                    the exception is in thread main so i shouldn't look somewhere else for it ?
                    Your exception isn't thrown in your main method; it is thrown somewhere in the
                    same thread as the one in which the main method executed. Read the message
                    carefully: it mentions the method and line number where the exception was
                    thrown. It most certainly was not in your main method; it happened somewhere
                    where an array did have at most two elements because index 2 (see message)
                    was out of the array bounds.

                    or

                    alternatively that exception was thrown in the main method itself and you didn't
                    supply four arguments to your main method (less than three actually because
                    2 is an out of bound array index.

                    kind regards,

                    Jos
                    Last edited by JosAH; Jun 11 '07, 02:15 PM. Reason: forgot something

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #11
                      yes it's Producent producent=new Producent(args[0],args[1],args[2],args[3]); causing the exception on line 607
                      weird
                      it works fine with 2 parameters
                      but i need four for the queues

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        i bat i have 4 parameters
                        i checked it to be sure

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by oll3i
                          yes it's Producent producent=new Producent(args[0],args[1],args[2],args[3]); causing the exception on line 607 it worked fine with 2 parameters
                          Well, there you have your solution: you should pass four argument to your main()
                          method if your constructor expects four arguments; simple as that.

                          kind regards,

                          Jos

                          Comment

                          • blazedaces
                            Contributor
                            • May 2007
                            • 284

                            #14
                            Originally posted by JosAH
                            Well, there you have your solution: you should pass four argument to your main()
                            method if your constructor expects four arguments; simple as that.

                            kind regards,

                            Jos
                            He should still have error/exception handling if someone/him inputs less then 4 or maybe more arguments though right? ;)

                            -blazed

                            Comment

                            • oll3i
                              Contributor
                              • Mar 2007
                              • 679

                              #15
                              it must be something wrong in bat file cos when i run it
                              >java Producent q1 q2 q3 q4 it works and app opens

                              Comment

                              Working...