Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

methods that causes nullpointer exception

 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to return a null strategy...how do I do that

 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you wish return empty strategy then use this,
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the class is an enum..there fore I cannot instantiate it
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will this work
 
HemPushap Kaushik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO
This wont work.
It will throw a Null Pointer Exception when you are trying to return a value from MgCalcStrategy which is null.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enums only really give you two choices here. Either decide which of the enumerated values is the default and return that, or return null. You can't return an "empty" enumerated value. An empty value doesn't really have any meaning anyway - how is it any different from null (other than not requiring null checking before using)?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vuthlarhi donald:
and the class is an enum..there fore I cannot instantiate it
No, the class is not an enum. Enums are enums not classes, and classes are classes not enums. They are different.
You don't instantiate an enum; it is (as far as I remember) a collection ("enumerated type") of objects; they are already instantiated, but in a sort of "read-only" format.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could define a new value in your enum for such cases, e.g. MgCalcStrategy.NULL ou MgCalcStrategy.UNDEFINED
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic