473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

java.lang.ArrayIndexOutOfBoundsException

oll3i
679 512MB
i get
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 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

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args){
  2.  
  3.  
  4.  
  5.         if (args.length < 1 || args.length > 4) {
  6.             System.out.println("usage: <destination> <destination> <destination> <destination>");
  7.             System.exit(1);
  8.         }else {Producent producent=new Producent(args[0],args[1],args[2],args[3]);}
  9.          /// producent.close();
  10.  
  11.       }     
  12. }
  13.  
thank you
Jun 11 '07 #1
22 40955
r035198x
13,262 8TB
i get
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 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

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args){
  2.  
  3.  
  4.  
  5.         if (args.length < 1 || args.length > 4) {
  6.             System.out.println("usage: <destination> <destination> <destination> <destination>");
  7.             System.exit(1);
  8.         }else {Producent producent=new Producent(args[0],args[1],args[2],args[3]);}
  9.          /// producent.close();
  10.  
  11.       }     
  12. }
  13.  
thank you
The error will occur if args.length is 1, 2, or 3. Change your if condition.
Jun 11 '07 #2
oll3i
679 512MB
i changed it to
if (args.length < 1 || args.length > 3)but stiil get the exception
Jun 11 '07 #3
r035198x
13,262 8TB
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.
Jun 11 '07 #4
blazedaces
284 100+
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:
Expand|Select|Wrap|Line Numbers
  1. args.length == 1, it still tries to call args[1],2,3
  2.  
  3. args.length == 2, it still tries to call args[2],3
  4.  
  5. args.length == 3, it still tries to call args[3]
  6.  
  7. only with args.length == 4 or more is it able to call all of these...
  8.  
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
Jun 11 '07 #5
oll3i
679 512MB
.... i threw out the if
left only
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args){
  2.  
  3.  
  4.  
  5.  
  6.         Producent producent=new Producent(args[0],args[1],args[2],args[3]);
  7.  
  8.  
  9.  
  10.       }     
  11. }
  12.  
and i stil get the exception

thank you
Jun 11 '07 #6
r035198x
13,262 8TB
.... i threw out the if
left only
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args){
  2.  
  3.  
  4.  
  5.  
  6.         Producent producent=new Producent(args[0],args[1],args[2],args[3]);
  7.  
  8.  
  9.  
  10.       }     
  11. }
  12.  
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.
Jun 11 '07 #7
oll3i
679 512MB
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 ?
Jun 11 '07 #8
blazedaces
284 100+
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. IndexArrayOutOfBounds 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:
Expand|Select|Wrap|Line Numbers
  1. 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
Jun 11 '07 #9
JosAH
11,448 Expert 8TB
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
Jun 11 '07 #10
oll3i
679 512MB
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
Jun 11 '07 #11
oll3i
679 512MB
i bat i have 4 parameters
i checked it to be sure
Jun 11 '07 #12
JosAH
11,448 Expert 8TB
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
Jun 11 '07 #13
blazedaces
284 100+
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
Jun 11 '07 #14
oll3i
679 512MB
it must be something wrong in bat file cos when i run it
>java Producent q1 q2 q3 q4 it works and app opens
Jun 11 '07 #15
JosAH
11,448 Expert 8TB
He should still have error/exception handling if someone/him inputs less then 4 or maybe more arguments though right? ;)

-blazed
Yep; if the constructor for that class demands four arguments the user better
supply them all four, no more and no less and the code should elegantly bail
out if any other number of arguments is supplied. That entire little problem is
so old; an args.length != 4 test is all that's needed in this example.

kind regards,

Jos
Jun 11 '07 #16
r035198x
13,262 8TB
it must be something wrong in bat file cos when i run it
>java Producent q1 q2 q3 q4 it works and app opens
You just run the bat file without supplying the arguments?
Jun 11 '07 #17
oll3i
679 512MB
no in bat i 'm passing four arguments that's why i dont know why it happens
Jun 11 '07 #18
JosAH
11,448 Expert 8TB
no in bat i 'm passing four arguments that's why i dont know why it happens
Use the "poor man's debugger": do this at the start of your main method:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("nof args: "+args.length);
  2. for (int i= 0; i < args.length; i++)
  3.    System.out.println(i+": "+args[i];
  4.  
and see what arguments are really passed to your main method. Always give
the bug less and less space to hide; finally you can pinpoint it and remove it.
Just guessing never helped anyone.

kind regards,

Jos
Jun 11 '07 #19
blazedaces
284 100+
Use the "poor man's debugger": do this at the start of your main method:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("nof args: "+args.length);
  2. for (int i= 0; i < args.length; i++)
  3.    System.out.println(i+": "+args[i];
  4.  
and see what arguments are really passed to your main method. Always give
the bug less and less space to hide; finally you can pinpoint it and remove it.
Just guessing never helped anyone.

kind regards,

Jos
What's the "rich man's debugger" ?!

This is how I usually debug things...

-blazed
Jun 11 '07 #20
JosAH
11,448 Expert 8TB
What's the "rich man's debugger" ?!

This is how I usually debug things...

-blazed
The "rich man's debugger" has a gui and a tremendous amount of buttons to
click and you don't know which one because there are also a myriad of options
to select and you finally end up clicking that little 'X' at the top right corner and
insert a couple of System.out.println() method calls and try again ;-)

kind regards,

Jos
Jun 11 '07 #21
r035198x
13,262 8TB
The "rich man's debugger" has a gui and a tremendous amount of buttons to
click and you don't know which one because there are also a myriad of options
to select and you finally end up clicking that little 'X' at the top right corner and
insert a couple of System.out.println() method calls and try again ;-)

kind regards,

Jos
LOL. No wonder they say you're crazy.
Jun 11 '07 #22
JosAH
11,448 Expert 8TB
LOL. No wonder they say you're crazy.
Duh; I have a pink inflatable axe that goes *beep!*

kind regards,

Jos ;-)
Jun 11 '07 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
24
by: crazystone82 | last post by:
Hi all, please send me a source code to transfer a file to a server running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
2
by: bkquinn359 | last post by:
Hi All, I am getting the follwoing error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Chp8Assign1.main(Chp8Assign1.java:37) Press any key to continue... ...
2
by: ksheerasagar17 | last post by:
Hello All, Scenario: Sending an image through webservice as byte array to an Java webservice. The Problem1: The webservice method image property expects (data type) SByte rather than Byte...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.