In this line of code
<% var tmp = int.Parse(ViewData["numOfGroups"].ToString()); %>
I have error:Object reference not set to an instance of an object. How correctly convert
ViewData["numOfGroups"] to int?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should first make sure that your controller action is setting this variable:
Once you’ve done this you should no longer get a
NullReferenceExceptionand your code should work.Of course as I’ve already written it multiple times here you should prefer strongly typed view instead of
ViewData. Also you should type your model properties accordingly. It is not the responsibility of the view to parse strings. So:And in your view:
By the way this should also be avoided as I have the feeling that you are declaring variables in your view which means that you have the intent of using them. Views are not for declaring variables and writing C# code. They are markup.