I am trying to convert String to an enum value. but I am getting null pointer exception. Below is the code which I have tried to convert. (I have checked other stackoverflow questions before asking the question)
public static Object parseEnum(String enumString, Type enumType) {
Type cType = Type.forName(
String.format('List<{0}>', new List<String>{ enumType.getName() })
);
System.debug('cType ' + cType); // throws null pointer exception
return ((List<Object>) JSON.deserialize(
String.format('["{0}"]', new List<String>{ enumString }),
cType
))[0];
}
public enum Markup_Category {
Admin,
DC,
Frozen
}
this is my methods which I am using to convert from String to Enum type. I am calling below code in the anonymous window for testing.
String markupCategory = 'Admin';
Markup_Category enumBoniMarkupCategory = (Markup_Category) parseEnum(
markupCategory,
Markup_Category.class
);
It is throwing null pointer exception at System.debug();. If someone can help me what I am doing wrong or any other suggestions would be great.
PS: Please ignore any typo mistake as code is compelling correctly.
JSON.deserialize()
although Salesforce is reporting it on theSystem.debug()
. If you remove theJSON.deserialize()
, take a look at the output of your debug, I think you are getting a nullType
.