Custom Macro ( Null error, even though there is data)

Brandon R asked on November 7, 2018 16:30

We are getting errors from a custom macro that we created.

As soon as the macro is initiated there is value, however we are getting null errors in the event logs. The macro is a simple boolean flag (0 = false, 1 = true) and we are returning the value as string.

We are using the macro in the visible section in multiple webparts to either hide or show it based on the flag for our users.

{% FMacros.GetFlag() != "True" #%}

When this macro is executed, the user has already authenticated and they will have all the data to successfully run the macro. So the value should return either "true" or "false". In my dev environment I can't replicate the errors.

Bellow is the error.

Error while evaluating expression: FMacros.GetFlag() != "True"

System.NullReferenceException: Object reference not set to an instance of an (Macro Location) 
at SessionMacroContainer.GetVoterConfidentailFlag(EvaluationContext context, Object[] parameters)
at CMS.MacroEngine.ExpressionEvaluator.ExecuteMethod(IMacroMethod method, IEnumerable`1 parameters)
at CMS.MacroEngine.ExpressionEvaluator.EvaluateMethodCall(Boolean& match, Boolean& securityPassed)
at CMS.MacroEngine.ExpressionEvaluator.EvaluateInternal(Boolean& match, Boolean& securityPassed)
at CMS.MacroEngine.ExpressionEvaluator.EvaluateEquality(Boolean& match, Boolean& securityPassed
at CMS.MacroEngine.ExpressionEvaluator.EvaluateInternal(Boolean& match, Boolean& securityPassed)
at CMS.MacroEngine.ExpressionEvaluator.Evaluate()
at CMS.MacroEngine.MacroResolver.ResolveMacroExpression(ResolveExpressionSettings settings)

Thanks much!

Correct Answer

Peter Mogilnitski answered on November 7, 2018 17:02

Can you share the code? You have null reference exception i.e. some object is null and you trying to access its property or somehting.

P.S. Is there a reason you are not using Boolean so it will be more like {%!FMacros.GetFlag()|(identity)GlobalAdministrator%}?

0 votesVote for this answer Unmark Correct answer

Recent Answers

Brandon R answered on November 7, 2018 17:31

No reason we have multiple developers, and just probably copied from another working macro.

I plan to refactor the code. We are using dapper in helper classes to query the data I am not sure if that is part of the problem. Below is trhe macro

[MacroMethod(typeof(string), "Gets Confidential Flag", 1)]
[MacroMethodParam(1, "value", typeof(string), "x")]
public static string GetFlag(EvaluationContext context, params object[] parameters)

    ConfidentialHelper GetConfidentialSetting = new ConfidentialHelper();

    var settings = GetConfidentialSetting.GetConfidentailFlag().Confidential;

    return settings.ToString();

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on November 7, 2018 18:00

Might want to wrap a try catch in that method and log any errors, and double check that GetConfidentialSetting.GetConfidentailFlag() isn't null before calling Confidential off of it.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.