NullReferenceException when trying to set class property(尝试设置类属性时出现 NullReferenceException)
问题描述
基本上我有 2 个类,Manifest"和BrowserAction",如下所示:
Basically I have 2 classes, "Manifest" and "BrowserAction", set out like this:
public class BrowserAction
{
public string default_icon {get; set;}
public string default_title {get; set;}
public string default_popup {get; set;}
}
public class Manifest
{
public BrowserAction browser_action {get; set;}
}
问题是,当我尝试设置 Manifest 类的 browser_action.default_popup 的实例时,如下所示:
The problem is, that when I try to set an instance of the Manifest class' browser_action.default_popup, like this:
public void setManifest()
{
Manifest newManifest = new Manifest();
newManifest.browser_action.default_popup = "popup.html";
}
我得到一个 System.NullReferenceException.我环顾四周,但似乎找不到问题所在.它适用于Manifest"类的其他属性,这些属性只是字符串等.
I get a System.NullReferenceException. I've looked around but I can't seem to find what the problem is. It works fine for other properties of the "Manifest" class that are just strings etc.
如果相关,我的 IDE 是 MonoDevelop 2.4,我的框架是 Mono 2.6.7.
If it's relevant, my IDE is MonoDevelop 2.4, with Mono 2.6.7 for my framework.
推荐答案
在访问该实例的属性之前,您还必须初始化 browser_action
.
You'll have to intialize browser_action
too before accessing properties on that instance.
newManifest.browser_action= new BrowserAction();
这篇关于尝试设置类属性时出现 NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:尝试设置类属性时出现 NullReferenceException
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01