PeterJ
PeterJ

Reputation: 373

new PosExplorer() command throws null reference exception

I am trying to open a Posiflex USB cash drawer with C#. This the code (courtesy of someone else):

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

As soon as execution reaches the code:

explorer = new PosExplorer(), the following exception is thrown:

An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll.

If it helps, here is the stack trace:

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

Any help will be appreciated.

Upvotes: 5

Views: 2506

Answers (1)

Gh61
Gh61

Reputation: 9756

You see it in the stack trace:

PosExplorer on initilization checks weather you allowed it to send telemetry to Microsoft during instalation of POS for .NET. The code is trying to read this property:

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

So if you don't have POS for .NET instaled, this Subkey isn't in your registry and then exception is thrown.

Note:

Telemetry is contained only in 1.14 version of POS for .NET. I suggest to use version 1.12 which has same functions and is without telemetry.

Upvotes: 5

Related Questions