• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception with array of Integer  RSS feed

 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
I'm trying to insert numbers in a array of Integer neatly. When i try to insert the following numbers i have this exception.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at time.Historico.insere_ordenado(Historico.java:35)

Line 35 in my code is:
"while (num >= arry[i] && arry[i] != null && i < arry.length - 1)"

Numbers:
71 51 63 57 24 80 31
I have this exception when i try to instert 80.


What is wrong with this code?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us how you're implementing this method, because I'm not getting any errors.
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us how you're implementing this method, because I'm not getting any errors.


I'm sorry. I don't understand you. The implementation is above in my first post.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robson Martinz wrote:
I'm sorry. I don't understand you. The implementation is above in my first post.

You have defined a method, but you are not actually using it. Can you show us how you're using it?
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have defined a method, but you are not actually using it. Can you show us how you're using it?


Ok.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reorder your logic in (num >= arry[i] && arry[i] != null && i < arry.length - 1) so that you perform your null check on arry[i] before checking to see if the number is larger than the array element.
 
James Harte
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two points:

1) When you have a complex condition to check for it is always wisest to have the inner conditions delimited by parenthesis

Change
while (num >= arry[i] && arry[i] != null && i < arry.length - 1)
to
while ((num >= arry[i]) && (arry[i] != null) && (i < arry.length - 1) )

2) Check for null first

Change
while ((num >= arry[i]) && (arry[i] != null) && (i < arry.length - 1) )
to
while ((arry[i] != null) && (num >= arry[i]) && (i < arry.length - 1) )

- Jim
 
Robson Martinz
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, guys! Now everything is ok.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!