0

I have received an error

{"Object reference not set to an instance of an object."}

when trying to use a custom resolver (to force any Null returns to use a default set of values) like so :

public class AnswersResolver : IValueResolver<SurveyMonkey.Containers.Question, Question, QuestionAnswers>
{
    public QuestionAnswers Resolve(SurveyMonkey.Containers.Question source, Question destination, QuestionAnswers destMember, ResolutionContext context)
    {
        if (source.Answers != null)
        {
            QuestionAnswers q = Mapper.Map<QuestionAnswers>(source.Answers);
            return q;
        }
        else
        {
            QuestionAnswers q = new QuestionAnswers
            {
                Id = source.Id * 100000,
                Choices = {},
                Cols = { new Column {  Choices = {  } } },
                Other = new OtherAnswer { Text = "NO ANSWER" },
                Rows = { new Row {  Text = "NO ANSWER" } }
            };
            Choice c = new Choice { Description = "No ANSWER REQUIRED", Position = 0, Items = { }, QuestionAnswersId = q.Id.GetValueOrDefault(), QuestionAnswers = q };
            q.Choices.Add(c);
            q.Cols[0].Choices.Add(c);
            q.Cols[0].QuestionAnswers = q;
            q.Cols[0].QuestionAnswersId = q.Id.GetValueOrDefault();
            q.Rows[0].QuestionAnswers = q;
            q.Rows[0].QuestionAnswersId = q.Id.GetValueOrDefault();
            return q;
        }
    }
}

When I put in a break point I successfully cycle through several iterations, and the error is thrown at the Automapper.map command for Survey - which contains Question and contains Page which contains (some of) the same Question Objects.

EDIT: Error Message as requested

AutoMapper.AutoMapperMappingException was unhandled HResult=-2146233088 Message=Error mapping types.

Mapping types: Survey -> Survey SurveyMonkey.Containers.Survey -> SurveyMonkeyAPIv3.Survey

Type Map configuration: Survey -> Survey SurveyMonkey.Containers.Survey -> SurveyMonkeyAPIv3.Survey

Property: Questions Source=Anonymously Hosted DynamicMethods Assembly StackTrace: at lambda_method(Closure , Object , Object , ResolutionContext ) at SurveyMonkeyAPIv3.Program.DefinedSurveyCalls(SurveyContext Survey, String APIKey, String Token, Int64[] ValidSurveys) in C:\Surveymonkeytake2\SurveyMonkeyAPIv3\Program.cs:line 77 at SurveyMonkeyAPIv3.Program.Main(String[] args) in C:\Surveymonkeytake2\SurveyMonkeyAPIv3\Program.cs:line 41 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: HResult=-2146233088 Message=Error mapping types.

Mapping types: Question -> Question SurveyMonkey.Containers.Question -> SurveyMonkeyAPIv3.Question

Type Map configuration: Question -> Question SurveyMonkey.Containers.Question -> SurveyMonkeyAPIv3.Question

Property: Answers Source=Anonymously Hosted DynamicMethods Assembly StackTrace: at lambda_method(Closure , Object , Object , ResolutionContext ) InnerException: HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=SurveyMonkeyAPIv3 StackTrace: at SurveyMonkeyAPIv3.AnswersResolver.Resolve(Question source, Question destination, QuestionAnswers destMember, ResolutionContext context) in C:\Surveymonkeytake2\SurveyMonkeyAPIv3\Program.cs:line 854 at lambda_method(Closure , Object , Object , ResolutionContext ) InnerException:

There is no text after that last inner exception

Steve Wallace
  • 104
  • 1
  • 1
  • 12

1 Answers1

0

I've been unable to solve this issue, however I worked around it by setting the references to nullable, adding

.ForMember(T => T.Answers, s => s.Condition(so=> so!=null ))

to the CreateMap for Question and (after some fiddling around with primary keys), returned a converted set of data.

Steve Wallace
  • 104
  • 1
  • 1
  • 12