BarcodeManager null pointer exception on EMDK 4.2

// Expert user has replied.
n none none 3 years 6 months ago
163 2 0

Good morning.
 
I'm having some troubles trying to instantiate the BarcodeManager. Im following this example:http://techdocs.zebra.com/emdk-for-android/4-0/tutorial/tutBasicScannin…
 
The problem is that, when i try to instantiate the BarcodeManager in the *onOpen* method (barcodeManager = (BarcodeManager)emdkManager.getInstance(EMDKManager.FEATURE_TYPE.BARCODE);) a NullPointerException is thrown.
 
I created the following class:
public class ScannerZebra implements EMDKListener,
                                                                  StatusListener,
                                                                  DataListener {
 
    private String TAG = getClass().getName();
 
    private Context context;
    private EMDKManager emdkManager = null;
    private BarcodeManager barcodeManager = null;
    private Scanner scanner = null;
 
 
    public ScannerZebra(Context context) {
        this.context = context;
 
        EMDKResults results = EMDKManager.getEMDKManager(context, this);
        if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) {
            Log.i(TAG, "Error inicializando EMDK");
        }
    }
 
    // Method to initialize and enable Scanner and its listeners    private void initializeScanner() throws ScannerException {
        if (scanner == null) {
            // Get the Barcode Manager object            barcodeManager = (BarcodeManager) emdkManager.getInstance(EMDKManager.FEATURE_TYPE.BARCODE);
            // Get default scanner defined on the device            scanner = barcodeManager.getDevice(BarcodeManager.DeviceIdentifier.DEFAULT);
            // Add data and status listeners            scanner.addDataListener(this);
            scanner.addStatusListener(this);
            // Hard trigger. When this mode is set, the user has to manually            // press the trigger on the device after issuing the read call.            scanner.triggerType = Scanner.TriggerType.HARD;
            // Enable the scanner            scanner.enable();
            // Starts an asynchronous Scan. The method will not turn ON the            // scanner. It will, however, put the scanner in a state in which            // the scanner can be turned ON either by pressing a hardware            // trigger or can be turned ON automatically.            scanner.read();
        }
    }
 
    @Override    public void onOpened(EMDKManager emdkManager) {
        try {
            initializeScanner();
            Log.i(TAG, "ZEBRA inicializado");
        } catch (ScannerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Override    public void onClosed() {
 
    }
 
    @Override    public void onData(ScanDataCollection scanDataCollection) {
        Log.i(TAG, "Datos leídos: " + scanDataCollection.getResult().toString());
    }
 
    @Override    public void onStatus(StatusData statusData) {
 
    }
}
 
Im making the instance on a onCreate Activity method: scannerZebra = new ScannerZebra(getApplicationContext());
 
Searching in this forums i found the following link:
https://developer.zebra.com/thread/31555
BarcodeManager null referenceIt seems the same problem, so i downloaded again the EMDK4.2 libs and reinstalled them (from here: https://portal.motorolasolutions.com/Support/US-EN/Resolution?solutionI…), but this is not solving the problem.
 
What can be happening?
 
Im using Android Studio 2.1.2 over Mac OSX 10.11.5 with a TC75.
 
Thanks and regards.

Please Register or Login to post a reply

2 Replies

P Pietro Francesco Maggi

Hi none,
first a question regarding your device. Have you installed the EMDK v4.2 runtime on your TC75 as explained in the online documentation?

TC75 already includes an EMDK runtime but that is usually aligned to EMDK v3.1, if you're using newer features you may encounter some issues.

Said that, if I understand correctly your code, you're trying to use the EMDK not from an activity but from a wrapper object (something similar to what is done in this discussion).

Which context are you passing to the constructor of your ScannerZebra class?

Ciao
~Pietro

n none none

Hello Pietro.

Yes, the device wasnt upgraded. Thank you!

Regards

PS: I copied the wrapper code wrong.

In the onOpen method, after cleaning comments i deleted a row. The correct code is this one :
    @Override    public void onOpened(EMDKManager emdkManager) {
        this.emdkManager = emdkManager;
        try {
            initializeScanner();
            Log.i(TAG, "ZEBRA inicializado");
        } catch (ScannerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

CONTACT
Can’t find what you’re looking for?