发生System.NullReferenceException

时间:2014-12-22 12:38:42

标签: class oop initialization windows-phone-8.1

我必须为Question.cs

中的变量赋值
namespace Project.Classes.DataTypes
{
    public class Question_SubType
    {
        public int TotalNoOfAnswer { get; set; }
        public int TotalNoOfViews { get; set; }
        public string Category { get; set; }
        public string PostedDateTime { get; set; }
        public string UserName { get; set; }
        public string QuestionText { get; set; }
        public string Subject { get; set; }
        public int QuestionId { get; set; }
    }

    public class Question
    {
        public Question_SubType question { get; set; }
        public string Email { get; set; }
        public bool AskAsAnonymous { get; set; }
    }
}

在我的MainPage.xaml.cs中,我的代码会给我错误:

Question q1 = new Question();
q1.Email = "hello@hello.com"; //Works Fine
q1.question.QuestionId = 10;  //Gives Error
q1.question.Subject = "this is my question subject?";  //Gives Error

虽然我看到AskAsAnonymousnullEmailnull,但question也为null。所以它给了我错误:

  

类型' System.NullReferenceException'的例外情况发生在Project.exe中但未在用户代码中处理

我需要在Question.csMainPage.xaml.cs进行哪些更改?

0 个答案:

没有答案