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.

tereško
  • 56,151
  • 24
  • 92
  • 147
Ethan
  • 1,477
  • 1
  • 21
  • 36
  • in what line do you get the error? return View("SurveyQuestionType" + question.QuestionType, question); here? – DanielVorph Jul 27 '15 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. – Craig W. Jul 27 '15 at 23:29
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Craig W. Jul 27 '15 at 23:30
  • so.....I need to do all this within one method? – Ethan Jul 27 '15 at 23:30
  • No, it need not be one method, you just should not use `RedirectToAction()` in this context. – Peter Gluck Jul 27 '15 at 23:46

2 Answers2

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.

Peter Gluck
  • 7,914
  • 1
  • 36
  • 35
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......
    }
Raj Karri
  • 553
  • 4
  • 19
  • `TempData[]` could be used to persist the object across invocations, but it is not necessary for this use case. – Peter Gluck Jul 28 '15 at 00:11