新的PosExplorer()命令抛出空引用异常

时间:2015-09-03 12:08:09

标签: c#

我正在尝试使用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(),抛出以下异常:

mscorlib.dll中出现未处理的“System.NullReferenceException”类型异常。

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

 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()

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

你在堆栈跟踪中看到它:

PosExplorer启动检查天气允许它在安装POS for .NET期间向微软发送遥测。 代码试图读取此属性:

RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\POSfor.Net\\Setup").GetValue("CeipOptIn", (object) 0);

因此,如果您没有适用于.NET的POS,则此Subkey不在您的注册表中,然后抛出异常。

注意:

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