0

So I am really confused. I have some code:

public ActionResult submitSurveyQuestion(SurveyQuestion model) 
    {
        SurveyQuestion nextQuestion = new SurveyQuestion();
        nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
        return RedirectToAction("generateViewForSurveyQuestion", new { question = nextQuestion });
    }

public SurveyQuestion submitSurveyQuestionAndGetNextQuestionFromQuestion(SurveyQuestion currentQuestion)
    {
        SurveyQuestion surveyQuestion = new SurveyQuestion();
        surveyQuestion.Question = "question";
        //...etc, this just sets all the question properties
        return surveyQuestion;
    }
public ActionResult generateViewForSurveyQuestion(SurveyQuestion question)
    {
        //ERROR BELOW THIS LINE
        return View("SurveyQuestionType" + question.QuestionType, question);
        //ERROR ABOVE THIS LINE
    }

But for some reason, my code returns the error:An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object. When looking through the debugger, it says that question = null, but I set all the properties of question and instantiate it, so I am really confused as to what's going wrong here.....any guidance would be greatly appreciated.

5
  • in what line do you get the error? return View("SurveyQuestionType" + question.QuestionType, question); here? Commented Jul 27, 2015 at 23:23
  • 2
    You're using a RedirectToAction to hit the controller method. That sends a 302 response to the browser which then sends a request back to your controller. The nextQuestion object you created in submitSurveyQuestion is long gone by that point. Commented Jul 27, 2015 at 23:29
  • 1
    possible duplicate of What is a NullReferenceException and how do I fix it? Commented Jul 27, 2015 at 23:30
  • so.....I need to do all this within one method? Commented Jul 27, 2015 at 23:30
  • No, it need not be one method, you just should not use RedirectToAction() in this context. Commented Jul 27, 2015 at 23:46

2 Answers 2

4

You should call generateViewForSurveyQuestion() directly to return the view:

public ActionResult submitSurveyQuestion(SurveyQuestion model) {
  SurveyQuestion nextQuestion = new SurveyQuestion();
  nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
  return generateViewForSurveyQuestion(nextQuestion);
}

The overload of RedirectToAction() which you are invoking requires route parameters, which your SurveyQuestion object cannot properly represent.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use TempData as below.

public ActionResult submitSurveyQuestion(SurveyQuestion model) 
    {
        SurveyQuestion nextQuestion = new SurveyQuestion();
        nextQuestion = submitSurveyQuestionAndGetNextQuestionFromQuestion(model);
      TempData["question"] = nextQuestion;
        return RedirectToAction("generateViewForSurveyQuestion");
    }

public SurveyQuestion submitSurveyQuestionAndGetNextQuestionFromQuestion(SurveyQuestion currentQuestion)
    {
        SurveyQuestion surveyQuestion = new SurveyQuestion();
        surveyQuestion.Question = "question";
        //...etc, this just sets all the question properties
        return surveyQuestion;
    }
public ActionResult generateViewForSurveyQuestion()
    {
        // TempDate[question] available here......
    }

1 Comment

TempData[] could be used to persist the object across invocations, but it is not necessary for this use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.