Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I am new in creating POS systems.

I want to open my cash drawer when printing starts. I have found some codes in the internet but they do not seem to work. It always gives me this error: Object reference not set to an instance of an object.

This is my codes:
C#
using Microsoft.PointOfService;

CashDrawer myCashDrawer = null;
       PosExplorer explorer;

        public void CashDrawerClass()
        {
            try
            {
                explorer = new PosExplorer();
                DeviceInfo ObjDevicesInfo = explorer.GetDevice("C4141");
                bool check = ObjDevicesInfo.IsDefault;
                myCashDrawer = (CashDrawer)explorer.CreateInstance(ObjDevicesInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("CashDrawerClass: " + ex.Message);
            }
        }

        public void OpenCashDrawer()
        {
            try
            {
                myCashDrawer.Open();
                myCashDrawer.Claim(1000);
                myCashDrawer.DeviceEnabled = true;
                myCashDrawer.OpenDrawer();
                myCashDrawer.DeviceEnabled = false;
                myCashDrawer.Release();
                myCashDrawer.Close();
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("OpenCashDrawer: " + ex.Message);
            }
        }


The cash drawer is connected to my printer.
Posted

If you add some validation code in the constructor you might get more info.
Or use the debugger to step through the constructor line by line.

You should add some checks:

1. Is ObjDevicesInfo getting a value or is it null?
C#
if (ObjDevicesInfo == null)
   Report an error.


2. Check if myCashDrawer is given a value.
 
Share this answer
 
v2
Thats always the danger of copying bits of code off the net .. there's not a lot in the code you've shown, and you dont say where that error is emitted from, so ....

...ok, assuming the error comes from

C#
myCashDrawer.Open();


when you call OpenCashDrawer(), the most obvious thing, apart from it looking a bit messy design/implementation-wise, is I dont see anything that looks like

C#
myCashDrawer = new CashDrawer();


possibly with arguments being passed into the instantiation of the CashDrawer Object
 
Share this answer
 
Comments
George Jonsson 22-Oct-15 0:07am    
There is actually a line "myCashDrawer = (CashDrawer)explorer.CreateInstance(ObjDevicesInfo);" in the constructor.
Garth J Lancaster 22-Oct-15 0:18am    
good pickup - so the user actually creates an instance of CashDrawerClass() - so much for it being well structured/commented .. definately +points for your second point :-)
George Jonsson 22-Oct-15 0:39am    
Maybe, maybe not as we don't know where the error occurs.
However, using the info in both our solutions should solve the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900