新的PosExplorer()命令引发空引用异常。

5
我正在尝试使用C#打开一个Posiflex USB现金抽屉。这是代码(由他人提供):
using System;
using System.Collections.Generic;
using Microsoft.PointOfService;

namespace MyNamespace
{
  public class CashDrawerClass
  {
    CashDrawer myCashDrawer;
    PosExplorer explorer;

    public CashDrawerClass()
    {
        explorer = new PosExplorer();
        DeviceInfo ObjDevicesInfo = explorer.GetDevice("CashDrawer", "RR-Drawer");
        myCashDrawer = explorer.CreateInstance(ObjDevicesInfo) as CashDrawer;
    }

    public void OpenCashDrawer()
    {
        myCashDrawer.Open();
        myCashDrawer.Claim(1000);
        myCashDrawer.DeviceEnabled = true;
        myCashDrawer.OpenDrawer();
        myCashDrawer.DeviceEnabled = false;
        myCashDrawer.Release();
        myCashDrawer.Close();
    }
  }
}

一旦执行到代码:

explorer = new PosExplorer(),就会抛出以下异常:

未经处理的类型为'System.NullReferenceException'的异常在mscorlib.dll中发生。

如果有帮助的话,这是堆栈跟踪:

 at Microsoft.PointOfService.Pos4NetTelemetry.IsCeipOptInEnabled()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Microsoft.PointOfService.Pos4NetTelemetry.get_Enabled()
   at Microsoft.PointOfService.Pos4NetTelemetry.SetCurrentProcessBitness()
   at Microsoft.PointOfService.PosExplorer.Initialize()
   at Microsoft.PointOfService.PosExplorer..ctor()
   at RunningRabbit.CashDrawerClass..ctor() in c:\C# Development\RunningRabbit\RunningRabbit\CashDrawerClass.cs:line 14
   at RunningRabbit.MainPOS.btnOpenDrawer_Click(Object sender, EventArgs e) in c:\C# Development\RunningRabbit\RunningRabbit\MainPOS.cs:line 261
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
   at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at RunningRabbit.Program.Main() in c:\C# Development\RunningRabbit\RunningRabbit\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

任何帮助将不胜感激。

Soner,我对空引用的概念有所了解,但是在这个具体的情况下,我无法弄清楚为什么会出现错误。 - PeterJ
我们能看到你的 PosExplorer 吗? - Soner Gönül
PosExplorer是Microsoft.PointOfService库的一部分。 - Pedro Isaaco
据我所知,PosExplorer是PointOfService库中的一个类,因此应该像代码中那样实例化。 - PeterJ
1
@PeterJ 你有没有找出为什么会发生这种情况?我也遇到了同样的问题,但只发生在一个特定的电脑上——代码在其他许多机器上都能正常工作。 - James Reategui
显示剩余4条评论
1个回答

5
你能在堆栈跟踪中看到它:
PosExplorer 在初始化时检查您是否在 POS for .NET 安装期间允许向 Microsoft 发送遥测数据。 代码正在尝试读取此属性:
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\POSfor.Net\\Setup").GetValue("CeipOptIn", (object) 0);

如果您没有安装 POS for .NET,那么该子键不在您的注册表中,然后将抛出异常。

注意:

遥测功能仅包含在 POS for .NET 1.14版本 中。 我建议使用版本 1.12,它具有相同的功能并且没有遥测功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接