我有两节课:
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();
}
}
}
错误消息:
未处理的异常:System.NullReferenceException:对象引用 未设置为未处理的异常:System.NullReferenceException:对象 引用未设置为对象的实例。在 fuel_sdk_test.PTS_Controller.openCom()在 C:\ Users \ user \ RiderProjects \ fuel_sdk_test \ fuel_sdk_test \ PTS_Controller.cs:line 44 at fuel_sdk_test.Program.Main(String [] args)在 C:\ Users \ user \ RiderProjects \ fuel_sdk_test \ fuel_sdk_test \ Program.cs:line 8
答案 0 :(得分:4)
您永远不会初始化“ pts” 尝试在构造函数中初始化它,我认为它应该解决