调用任何方法时 - 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例

When Call Any Method - Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object

我有两个类:

PTS_Controller

using System;
using TiT.PTS;

namespace fuel_sdk_test
{
    public class PTS_Controller
    {
        private PTS pts;
        private int? pumpNumber;
        private byte? nozzleNumber;
        private int? pricePerLiter;
        private int? orderDose;
        private int[] prices;
        private int? transactionId;
        private int? atgNumber;
        private short parameterAddress;
        private byte[] parameterValue;
        private int parameterNumber;
        private bool authorizeVolume;
        private bool extendedCommands;
        private int? normalResponseTimeout = 100;
        private int? middleResponseTimeout = 200;
        private int? longResponseTimeout = 400;

        private static readonly PTS_Controller instance = new PTS_Controller();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static PTS_Controller()
        {
        }

        private PTS_Controller()
        {
        }

        public static PTS_Controller Instance
        {
            get
            {
                return instance;
            }
            set
            {
            }
        }

        public void openCom()
        {
            if (!pts.IsOpen)
            {
                try
                {
                    pts.PortName = "COM6";
                    pts.Open();
                    Console.WriteLine("Port opened successfully");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

还有我的Programs.cs

namespace fuel_sdk_test
{
    internal class Program
    {
        public static void Main(string[] args)
        {
                PTS_Controller.Instance.openCom();
        }
    }
}

错误信息:

Unhandled Exception: System.NullReferenceException: Object reference not set to Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at fuel_sdk_test.PTS_Controller.openCom() in C:\Users\user\RiderProjects\fuel_sdk_test\fuel_sdk_test\PTS_Controller.cs:line 44 at fuel_sdk_test.Program.Main(String[] args) in C:\Users\user\RiderProjects\fuel_sdk_test\fuel_sdk_test\Program.cs:line 8

你从不初始化“pts” 尝试在构造函数中初始化它我认为它应该解决它