0

Here I am developing an adroid app which will read the records in the NFC tag. Here is the part of code in my program to read the tag, when it read, Out of bounds exception occurs.

NdefMessage[] msgs = null;
    String action = intent.getAction();
    if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)||
            NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if(rawMsgs != null){
            msgs = new NdefMessage[rawMsgs.length];
            for(int i=0; i<rawMsgs.length; i++){
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }else{
            byte[] empty = new byte[]{};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
            msgs = new NdefMessage[]{msg};
        }

    }
    if(msgs==null){
        String[] array = new String[]{"No Tag discovered!"};
        return array;
    }else{
        String[] array = new String[]{new String(msgs[0].getRecords()[0].getPayload()), 
                                        new String(msgs[1].getRecords()[0].getPayload())
        };
        return array;
    }

Records in tag

When I hide this sentence "new String(msgs1.getRecords()[0].getPayload())", my program can run smoothly, so I am quite sure program is in this line. May I ask how can I solve the problem?

Conrad
  • 933
  • 4
  • 19
  • 34
  • I have tried to output some values both length of rawMsgs and Msgs[0].getReord() are of length 1, may I ask where is the second record? – Conrad Jul 17 '12 at 08:54

1 Answers1

1

Yes problem is sure in this line

new String[]{new String(msgs[0].getRecords()[0].getPayload()), 
                                            new String(msgs[1].getRecords([0].getPayload())

because you are try to access msgs[1].getRecords in above line. But if you see your code, in some condition like

else{
            byte[] empty = new byte[]{};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
            msgs = new NdefMessage[]{msg};
        }

msgs arrays contains only one element but you try to access 2nd element which will be not exist. So change your code and apply condition before access the array.

Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
  • I have finally tried it out, I can access the second one by msgs[0].getRecord()[1].getPayload(). I would like to ask one more questions, is there any chance msgs array contain more than one element? Because I write out this program by following some examples, there are some codes I dont quite understand at all. – Conrad Jul 17 '12 at 09:07
  • you are using Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if size of rawMsgs is more than 1 then msgs array contain more than one element. – Vivek Kumar Srivastava Jul 17 '12 at 09:12
  • but in your case rawMsgs array having Null – Vivek Kumar Srivastava Jul 17 '12 at 09:13
  • I have tried to output the length of rawMsgs before, and it displays 1 to me – Conrad Jul 17 '12 at 09:17
  • so in my case, the program should get the message in the else way instead of the rawMsgs way if it is correct? – Conrad Jul 17 '12 at 09:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13982/discussion-between-vivek-kumar-srivastava-and-user1502740) – Vivek Kumar Srivastava Jul 17 '12 at 09:26