0

I am developing an NFC based android application. I get an arrayIndexOutOfBoundsException length = 0 index = 0 error when sending the object with Message with the Handler class.

Process: com.telpo.tps530_demo, PID: 8949
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at com.telpo.tps530_demo.NFCDesfire$MyHandler.handleMessage(NFCDesfire.java:169)
    

Handler Class

 private class MyHandler extends Handler{
    private WeakReference<NFCDesfire> activityWeakReference;
    public MyHandler(NFCDesfire activity){
        activityWeakReference = new WeakReference<>(activity);
    }

Override

@Override
    public void handleMessage(Message msg) {
        NFCDesfire activity = activityWeakReference.get();
        Context context = activity.getApplicationContext();
        if(activity!=null) {
            switch (msg.what) {
                case CHECK_NFC_TIMEOUT: {
                    Toast.makeText(context, "TIME OUT !", Toast.LENGTH_LONG).show();
                    activity.nfc_open_btn.setVisibility(4);
                    activity.nfc_close_btn.setVisibility(4);
                    activity.nfc_check_btn.setVisibility(4);
                }
                break;
                case SHOW_NFC_DATA: {
                   byte[] uid_data = (byte[]) msg.obj;
                    if (uid_data[0] == 0x41) {
                        Toast.makeText(NFCDesfire.this,"Başarılı",Toast.LENGTH_SHORT).show();

                        byte[] atqa = new byte[2];
                        byte[] sak = new byte[1];
                        byte[] uid = new byte[uid_data[5]];

                        System.arraycopy(uid_data, 2, atqa, 0, 2);
                        System.arraycopy(uid_data, 4, sak, 0, 1);
                        System.arraycopy(uid_data, 6, uid, 0, uid_data[5]);

                        Log.e("yw", " " + atqa[0] + "  " + atqa[1] + "  " + sak[0]);
                        islemBitti = false;

Error Line

uid_data[0] == 0x41

What am I supposed to do here? Anyone have a solution? There is a continuation of the codes, so I can share the source code if desired.

1 Answers1

0

You must check the length of msg.obj before any processing;it seems that msg.obj is empty and you are trying to access it's first element so it throws ArrayIndexOutOfBoundsException.