• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

java.lang.ArrayIndexOutOfBoundsException:-1

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! From this code i got an exception: java.lang.ArrayIndexOutOfBoundsException:-1

I guess the problem came from here: from 240 to 253 lines




Could anyone tell me what's the problem?
 
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing caseSQL.length = 0
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately it's not working
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is not working?
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the code to this

 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the full code:

https://data.hu/get/11908790/ManualQuery.zip
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

caseSQL[caseSQL.length]


This will always throw an exception - the maximum allowable index into an array is its length - 1. You just need to check that the length is not 0.

...data.hu/get/...


I won't download ZIP files from random hosts, and I'm guessing that few people will.
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ombodi Gergő wrote:I modified the code to this. . .

Don't simply modify code. That looks as if you were guessing. Work out waht the error is and only when you know that should you modify any code.
Why are you using |= when you could use a BitSet?
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what the problem is. I don't know how to fix it and where.
I'm a little bit of beginner in java :/
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did my previous post make sense to you? In it I stated what the problem is, and how to avoid it.
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how can i check it?
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhat like this:

if (caseSQL.length == 0)
    return something;
else
   return something else;
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it.
It's not 0
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means ArrayIndexOutOfBoundsException does no longer happen, and the problem is solved? If not, post the code you're now using, and what exception you're now getting (and indicate in which line of code).
 
Ombodi Gergő
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is same.
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the problem is now slightly different. If you have a 0‑length array, it doesn't have an element at index 0. In fact line 13 is trying to find the element at the index equal to the array's length, which can be relied on to throw an out of bounds exception if that line is actually executed.
Please explain what that method is supposed to do, because it is confused code.
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:If not, post the code you're now using, and what exception you're now getting (and indicate in which line of code).



Try a bit harder to help us help you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!