• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException  RSS feed

 
Abdoulgani Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i am abeginner in java and i some HELP.  Here is my code... Oh and i am a French so excuse my english    


First Cass Mabase contains any database interaction and connections





The second class is my UI components



My Log Error


 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us which line in your code that Exception arises from.
Please look what Lists or arrays are used in that line; an index of 0 means you are trying to use an empty array or List, which has no elements, and get its first element.
In future please use correct indentation; look at your lines 33‑35 for example.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Add some debugging in there to show how big these two arrays actually are.
 
Abdoulgani Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Add some debugging in there to show how big these two arrays actually are.


??? how to do that ? i am using netbeans
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdoulgani Mohamed wrote:. . . i am using netbeans
That shou‍ldn't make any difference. If you know how to use the NetBeans debugger, that will help, but start by inserting a line similar to this before that line:-

I think I was mistaken about an empty array; I read the exception wrongly and thought it says index 0. Sorry.
You have written the for loop incorrectly. Remind yourself of the correct format for a for loop to iterate an array. If you are using i for the nomColonne index, will it be the same for the other array? Why are you using parallel arrays in the first place? That is not at all object‑oriented coding. Create a class to encapsulate all the data, and methods to return those data in a form suitable for adding to the SQL.
 
Matt Wong
Greenhorn
Posts: 23
3
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it easy to spot the error in this case, here is another helpful hint:

catch()-blocks should always contain a line that looks something like this:

instead of just out./err.print().

Why? While your code would at least show there's an error in this block, it won't tell you the cause that led to it.
As the trace you printed, its the 2nd line showing what went wrong, but 1st line on its own wouldn't helped at all.

What's your problem: you're tryin to access two different arrays wich seem to differ in size with the same index based on one of them. So when the first array is larger than the second on you run into the problem that you're tryin to access an information wich is not available. you should add a check to at least check if both arrays has the same length.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wong wrote:. . . catch()-blocks should always contain a line that looks something like this:

instead of just out./err.print().
Good point. Maybe you would consider changing that later; printStackTrace is really useful for the developer, but gives an incomprehensible output for end‑users. Maybe one could write something different in the catch before letting the app loose on an unsuspecting world. Just maybe.
. . . check if both arrays has the same length.
That for loop can be relied on to throw an exception irrespective of the sizes of the two arrays.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That for loop can be relied on to throw an exception irrespective of the sizes of the two arrays.


I'm not sure it will if the arrays are the same length.


OK, it's not a regular way of writing it, but that '- 1' makes it the same as an 'i < nomColonne.length'.
Or have I missed something else?

In any case, the key take away from this is (as you said) parallel arrays aren't the best idea.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I missed the − 1. I was completely mistaken there
It still ain't the right way to write a for loop.
 
Abdoulgani Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Wong wrote:As it easy to spot the error in this case, here is another helpful hint:

catch()-blocks should always contain a line that looks something like this:

instead of just out./err.print().

Why? While your code would at least show there's an error in this block, it won't tell you the cause that led to it.
As the trace you printed, its the 2nd line showing what went wrong, but 1st line on its own wouldn't helped at all.

What's your problem: you're tryin to access two different arrays wich seem to differ in size with the same index based on one of them. So when the first array is larger than the second on you run into the problem that you're tryin to access an information wich is not available. You should add a check to at least check if both arrays has the same length.


I used the try and Catch then :
 
Abdoulgani Mohamed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i don't use the function MaBase UpdateQuery ? and i made a connection in the listener of Update Button ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!