【问题标题】:Object Reference not set when using PrinterSettings.StringCollection使用 PrinterSettings.StringCollection 时未设置对象引用
【发布时间】:2015-10-22 00:39:37
【问题描述】:

我正在尝试使用PrinterSettings.StringCollection 列出当前安装的打印机。但是,我收到此错误:

对象引用未设置为对象的实例

代码如下:

namespace DropDownLibrary
{
    public class DropDownExample : DSDropDownBase
    {
        public DropDownExample() : base("item") { }

        public static PrinterSettings.StringCollection InstalledPrinters { get; }

        public override void PopulateItems()
        {
            // The Items collection contains the elements
            // that appear in the list.            

            Items.Clear();

            // Create a number of DynamoDropDownItem objects 
            // to store the items that we want to appear in our list.

            var newItems = new List<DynamoDropDownItem>();
            {
                 foreach (String name in InstalledPrinters)
                 {
                     new DynamoDropDownItem("{0}", name);
                 }
            };

            Items.AddRange(newItems);

            // Set the selected index to something other
            // than -1, the default, so that your list
            // has a pre-selection.

            SelectedIndex = 0;
        }

        public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
        {
            // Build an AST node for the type of object contained in your Items collection.

            var intNode = AstFactory.BuildIntNode((int)Items[SelectedIndex].Item);
            var assign = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), intNode);

            return new List<AssociativeNode> { assign };
        }
    }
}

【问题讨论】:

  • 您不会在任何地方为 InstalledPrinters 赋值。
  • 我是真正的 C# 新手,不是 "public static PrinterSettings.StringCollection InstalledPrinters { get; }" 将字符串集合分配给已安装的打印机吗?

标签: c# object reference


【解决方案1】:

this post 对此进行了介绍。 “对象引用未设置为对象的实例”错误是由您尝试使用为空的变量引起的。例如,您可以通过执行以下操作得到空引用错误:

object nullObject = null;
nullObject.ToString():

在您的代码中,似乎从未设置过 InstalledPrinters 的值。

在你的代码到达这一行之前:

foreach (String name in InstalledPrinters)

看起来你从this link复制粘贴了这个:

public static PrinterSettings.StringCollection InstalledPrinters { get; }

这是您可以访问的 PrinterSettings 类的属性。你应该像这样访问它:

var installedPrinters = System.Drawing.Printing.PrinterSettings.InstalledPrinters;

foreach (String name in installedPrinters)

【讨论】:

  • 感谢 jyanks。我理解错误,我只是无法弄清楚它是如何发生的。感谢您的帮助!
  • 查看我刚刚更新的代码。您需要实例化 InstalledPrinters。您认为已安装打印机的列表来自哪里?
  • 在您的行中添加时,我收到错误消息“没有给出与 PrinterSettings.StringCollection.StringCollection(string[]) 所需的形式参数 'array' 相对应的参数”。我以为我可以使用 System.Drawing.Printing 访问系统上已安装的打印机..
  • 啊。因此,如果您正在查看this code,看起来就像是您复制粘贴的内容,您实际上应该通过System.Drawing.Printing.PrinterSettings.InstalledPrinters 引用它。我不知道你是否可以从 asp.net 应用程序中做到这一点,但我会在可能的情况下更新我的答案。
  • 没错,尽管我确实根据您的回答调整了我的代码。我删除了“public static PrinterSettings.StringCollection InstalledPrinters { get; }”并在 foreach 行中添加了 System.Drawing.Printing.PrinterSettings.InstalledPrinters....我现在得到错误:索引超出范围,参数:我的索引认为是由于您所说的兼容性...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多