Search code examples
c#sdknullreferenceexceptionddlthermal-printer

Thermal Label SDK NullReferenceException


I am trying to create a program that will print to a thermal printer. I am using Visual Express 2010, C# with Thermal Label SDK from Neodynamic. I will note that I am not using the application itself, just adding the reference to the ddl file to use Thermal Label. I have followed some tutorials and resources around the web about how to get things working but I when I run the following code it throws this exception on line 131 (labelled in code):

System.NullReferenceException: Object reference not set to an instance of an object.

            //Define a label
            ThermalLabel tLabel = new ThermalLabel(UnitType.Cm, 8, 0);

            //Create labels items
            TextItem tTitle = new TextItem();
            tTitle.Text = "Yummy Yummy";
            tTitle.X = 0.5;
            tTitle.Y = 0.5;
            tTitle.Height = 0.5;
            tTitle.Width = 1;

            //Add items to the label
            tLabel.Items.Add(tTitle);

            //Create a PrintJob object
            PrintJob pj = new PrintJob();
            //Thermal printer is connected through parallel port
            pj.PrinterSettings.Communication.CommunicationType = CommunicationType.Parallel;
            //^^^^^^^^^^LINE 131^^^^^^^^^
            //Set thermal printer resolution
            pj.PrinterSettings.Dpi = 203;
            //Set thermal printer language
            pj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.EPL;
            //Set thermal printer parallel port name
            pj.PrinterSettings.Communication.ParallelPortName = "LPT1";
            //Set number of copies...
            pj.Copies = 2;
            //Print ThermalLabel object...
            pj.Print(tLabel);

What I have read about the NullReferenceExcpetion is that it occurs when something is "null". I understand this but as I am new to Thermal Label SDK I am not aware of what I am missing; what is currently "null" that I need to assign if you like. I've tried finding other examples of this problem but I cannot find anything.

Thanks in advance!


Solution

  • I have managed to fix my problem through some experimentation of my own.

    Even though this was never mentioned in any tutorial or documentation I could find, I found a solution that is quite simple. Before the PrintJob object is initialized I simply added this code to initialize a PrintSettings object...

    //Create a PrintSettings object
    PrinterSettings ps = new PrinterSettings();
    ps.Communication.CommunicationType = CommunicationType.Parallel;
    

    Changed this...

    PrintJob pj = new PrintJob();
    

    To this so that the PrintSettings object is passed to the PrintJob object...

    PrintJob pj = new PrintJob(ps);
    

    And removed this line since it was no longer needed...

    pj.PrinterSettings.Communication.CommunicationType = CommunicationType.Parallel;
    

    The thermal printer now prints whatever I want. Perfect. I hope this is helpful for other people.