Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 发生System.NullReferenceException异常_Class_Oop_Initialization_Windows Phone 8.1 - Fatal编程技术网

Class 发生System.NullReferenceException异常

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

我必须为有问题的变量赋值。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
虽然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