• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in Junit ArrayIndexOutOfBounds, cannot seem to find source  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get an error "ArrayIndexOutOfBounds" in my Junit test on the following method:



The error occurs in the first while loop right in the if condition (if (!arrayWlanSsid[i + 1].getMac().equalsIgnoreCase(arrayWlanSsid[i].getMac())) )

Does anyone have a clue what it could be?
Thanks!
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index expression i + 1 is the most likely suspect. If the array has 0 elements or if i is equal to the last valid index in the array, then adding 1 will cause the index out of bounds exception.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I leave out the 1 then?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The index expression i + 1 is the most likely suspect. If the array has 0 elements or if i is equal to the last valid index in the array, then adding 1 will cause the index out of bounds exception.

Just tried it, has the same error.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println can be your best friend. print out what i, last, and the various array's lengths are, just before the line where you get the error.

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:System.out.println can be your best friend. print out what i, last, and the various array's lengths are, just before the line where you get the error.



how would I achive that here?
The point of the exercise is to find mac adresses according to their ssid from a list.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("i = %d, array.length = %d%n", i, myArray.length);

Remember that the usual error message for an array out of bounds exception tells you the wrong index: if you try index 5 in a 4‑element array, the error message will probably have 5 in.

And remember that index 5 points to the 6th element in an array.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error still happens:
"java.lang.ArrayIndexOutOfBoundsException: -1"
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I found the problem and it is not what I thought:

Check out line 2 the parameters of the searchFirst method. The first Parameter is arrayWlanMac when it should be arrayWlanSsid.

It now works and I have on more question to follow in the following post.

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a failure in Junit that is the following:

Java.lang.AssertionError: wrong SSID expected:<UDSSR Network> but was:<null>

The name of the test is german for "testGetSSIDUppercaseWritten

So I assume it is about upper and lower case?

Any hint would be helpful!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array index -1???
The one way I can think to get that error is if you have searched for something and not found it. Then you may get an index of -1. Obviously you cannot use -1 as a real array index.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Array index -1???
The one way I can think to get that error is if you have searched for something and not found it. Then you may get an index of -1. Obviously you cannot use -1 as a real array index.

That problem is solved, I just had another question above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!