Class 发生System.NullReferenceException异常
我必须为有问题的变量赋值。cs: 在我的MainPage.xaml.cs中,我有一些代码会给我带来错误:Class 发生System.NullReferenceException异常,class,oop,initialization,windows-phone-8.1,Class,Oop,Initialization,Windows Phone 8.1,我必须为有问题的变量赋值。cs: 在我的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 虽然deguggin
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
虽然degugging我观察到AskAsAnonymous是空的,Email是空的,但问题也是空的。所以它给了我一个错误:
Project.exe中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理
我需要在Question.cs或MainPage.xaml.cs中做哪些更改?您需要实例化Question的构造函数中的Question\u子类型Question或public Question{Question\u子类型questionObject;}尝试使用构造函数。干得好。我实例化的方法正确吗?这是一个声明,它不会创建实例,您需要:public Question{this.Question=new Question_SubType;}
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