0

I am attempting to list the currently installed printers using PrinterSettings.StringCollection. However, I get this error:

Object Reference not set to an instance of an object

Code is as follows:

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 };
        }
    }
}
spenibus
  • 4,079
  • 11
  • 23
  • 33
  • 1
    You don't assign a value to InstalledPrinters anywhere. – Eric J. Oct 22 '15 at 00:41
  • I'm real new to C# , isn't "public static PrinterSettings.StringCollection InstalledPrinters { get; }" assigning the stringcollection to installedprinters? – JohnDoeSmith Oct 22 '15 at 00:47

1 Answers1

2

This is covered in this post. The 'Object reference not set to instance of an Object" error is caused by you trying to use a variable that is null. For instance, you can get a null reference error by doing:

object nullObject = null;
nullObject.ToString():

In your code, it doesn't look like the value for InstalledPrinters ever gets set.

Before your code reaches this line:

foreach (String name in InstalledPrinters)

It looks like you copy pasted this from this link:

public static PrinterSettings.StringCollection InstalledPrinters { get; }

This is a property on the PrinterSettings class that you can access. You should access it like so:

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

foreach (String name in installedPrinters)
Community
  • 1
  • 1
jyanks
  • 2,266
  • 1
  • 17
  • 36
  • Thanks jyanks. I understand the error I just can't figure out how it's happening. Thanks for the help! – JohnDoeSmith Oct 22 '15 at 00:51
  • See the code I just updated. You need to instantiate InstalledPrinters. Where do you think the list of installed printers is coming from? – jyanks Oct 22 '15 at 00:52
  • I get the error "there is no argument given that corresponds to the required formal parameter 'array' of PrinterSettings.StringCollection.StringCollection(string[])" when adding in your line. I thought I could access the installed printers on my system using System.Drawing.Printing.. – JohnDoeSmith Oct 22 '15 at 01:05
  • Ah. So if you're looking at [this code](https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.installedprinters) which looks like is what you've copy pasted, you should actually reference it via `System.Drawing.Printing.PrinterSettings.InstalledPrinters`. I don't know if you can do that from an asp.net app, but I'll update my answer assuming it is possible. – jyanks Oct 22 '15 at 01:09
  • Yeh exactly, although I did adjust my code based on your answer. I removed "public static PrinterSettings.StringCollection InstalledPrinters { get; }" and added System.Drawing.Printing.PrinterSettings.InstalledPrinters in the foreach line.... I now get the error : Index was out of range, parameter: Index which I think is due to the compatibility as you say... – JohnDoeSmith Oct 22 '15 at 01:16
  • I'm guessing that that's the cause. The System.Drawing.Printing is a Win32 library. – jyanks Oct 22 '15 at 01:19
  • mm that's annoying, thanks for your help though mate, good to know I'm not going mental trying to get it to work – JohnDoeSmith Oct 22 '15 at 01:21
  • this might be a very beginner question jyanks but how can you tell the code is from an asp.net app? – JohnDoeSmith Oct 22 '15 at 02:15
  • `DSDropDownBase` is an asp.net class used for drawing the dropdowns, I thought. – jyanks Oct 22 '15 at 02:40
  • I see, when I go to the definition it appears as:namespace DSCoreNodesUI { public abstract class DSDropDownBase : NodeModel { protected DSDropDownBase(); protected DSDropDownBase(string outputName); public ObservableCollection Items { get; set; } public int SelectedIndex { get; set; } public static int ParseSelectedIndex(string index, IList items); public static string SaveSelectedIndex(int index, IList items); – JohnDoeSmith Oct 22 '15 at 02:44
  • Sorry about the shoddy comment formatting, i'm still getting used to this site. And thanks for your help, i'm just trying to make sure for certain that I can't do what i'm trying to achieve – JohnDoeSmith Oct 22 '15 at 02:46
  • What kind of project did you choose when you started coding in visual studio? – jyanks Oct 22 '15 at 16:38
  • I believe it is .NET Framework 4.5. Don't worry though mate, I managed to get it working and it's displaying all local and network printers which is just what I was after. Thanks for your help! – JohnDoeSmith Oct 24 '15 at 00:08