Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes methods that causes nullpointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "methods that causes nullpointer exception" Watch "methods that causes nullpointer exception" New topic
Author

methods that causes nullpointer exception

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
I want to return a null strategy...how do I do that

Jilesh Lakhani
Ranch Hand

Joined: Jul 26, 2006
Posts: 49



If you wish return empty strategy then use this,


-Jilesh
Universe and Knowledge has no bounderies
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
and the class is an enum..there fore I cannot instantiate it
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
will this work
HemPushap Kaushik
Greenhorn

Joined: May 10, 2005
Posts: 8
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

Joined: Apr 14, 2004
Posts: 10336

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)?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36575
    
  16
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

Joined: Feb 29, 2008
Posts: 119
You could define a new value in your enum for such cases, e.g. MgCalcStrategy.NULL ou MgCalcStrategy.UNDEFINED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods that causes nullpointer exception
 
Similar Threads
Can some pattern help here?
Defining a Constant
null strategy with enums/design patterns
inheritance and schema creation using JPA annotations
one to one unidirectional mapping in jpa