Thermal Label SDK NullReferenceException

问题描述 投票:0回答:1

我正在尝试创建一个将打印到热敏打印机的程序。我使用的是Visual Express 2010,C#和Neodynamic的Thermal Label SDK。我会注意到我没有使用应用程序本身,只是添加对ddl文件的引用以使用Thermal Label。我已经在网上关注了如何让事情正常工作的一些教程和资源,但是当我运行以下代码时,它会在第131行抛出此异常(在代码中标记):

System.NullReferenceException:未将对象引用设置为对象的实例。

            //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);

我读到的有关NullReferenceExcpetion的内容是,当某些内容为“null”时会发生。我理解这一点,但由于我是Thermal Label SDK的新手,我不知道我缺少什么;如果你愿意,我需要分配的当前为“null”。我试过找到这个问题的其他例子,但我找不到任何东西。

提前致谢!

c# sdk nullreferenceexception ddl thermal-printer
1个回答
0
投票

我通过自己的一些实验设法解决了我的问题。

即使在我能找到的任何教程或文档中都没有提到过,我发现了一个非常简单的解决方案。在初始化PrintJob对象之前,我只是添加了这段代码来初始化PrintSettings对象......

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

换了这个......

PrintJob pj = new PrintJob();

为此,将PrintSettings对象传递给PrintJob对象......

PrintJob pj = new PrintJob(ps);

由于不再需要,删除了这一行......

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

热敏打印机现在打印我想要的任何东西。完善。我希望这对其他人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.