0

Now I know this question has been asked a few times but I cant seem to find a specific solution. Seems to me that the workarounds are specific to each case so I ll ask put forward my case here.

I am extending a BroadcastReceiver that catches some specific messages from my system and processes it. Now I believe the electric circuit that sends the message is prone to a lot of noise or interference, which causes some errors in the message, which in turn can cause a NumberFormatException.

However when I catch the exception and print it out, it in turn generates a NullPointerException. And I , for the life of me, cant figure out why this is happening and how do I fix it. Code is as follows.

public class SmsReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent)
{
    int craneIndex = 0;
    int integrityCheck = 0;

    try{
        Bundle bundle = intent.getExtras();

        String str = new String();
        Object[] messages = (Object[])bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];

        for ( int i = 0; i < messages.length; i++ )
            sms[i] = SmsMessage.createFromPdu((byte[])messages[i]);


        for(SmsMessage iter:sms)
            str += iter.getMessageBody();


        if ( "Meter Msg Recieved".equals(str) )
        {
            Bundle toMainActBundle = new Bundle();
            Message toMainActMessage = new Message();

            String senderNumber = sms[0].getOriginatingAddress();
            toMainActBundle.putString("SenderAddress", senderNumber);

            toMainActMessage.setData(toMainActBundle);
            MainActivity.mQueryHandler.sendMessage(toMainActMessage);
        }
        else if (str.contains("RTG") && str.contains("MaEng"))
        {
            CraneDataEntry temp = new CraneDataEntry();
            String[] decompString = str.split("[- :,]");

            if (decompString[0] == null || decompString[1] == null){
                MainActivity.mErroneousCraneMessage.add(str);
                return;
            }

            if ( "RTG".equals(decompString [0]) ){
                temp.mEquipmentName = new String(decompString[0] + "-" + decompString[1]);

                if (decompString[2] != null && decompString[3] != null) {
                    temp.mEntryTimeStamp.set(0, 
                            Integer.parseInt(decompString[3].substring(2,4)), 
                            (decompString[3].charAt(4) == 'A') ? 
                                    Integer.parseInt(decompString[3].substring(0, 2)) : 
                                        12 + Integer.parseInt(decompString[3].substring(0, 2)), 
                            Integer.parseInt(decompString[2].substring(0, 2)), 
                            Integer.parseInt(decompString[2].substring(2, 4)), 
                            2000 + Integer.parseInt(decompString[2].substring(4, 6)));
                    integrityCheck++;
                }

                if (decompString[4] != null && decompString[5] != null)
                    if ( "MaEng".equals(decompString[4]) ) {
                        temp.mMainEngineHours = Float.parseFloat(decompString[5]);
                        integrityCheck++;
                    }

                if (decompString[7] != null && decompString[8] != null)
                    if ( "AxEng".equals(decompString[7]) || "UPS".equals(decompString[7]) ) {
                        temp.mAuxEngineHours = Float.parseFloat(decompString[8]);
                        integrityCheck++;
                    }

                if (decompString[10] != null && decompString[11] != null)
                    if ( "CP".equals(decompString[10]) ) {
                        temp.mCraneOnHours = Float.parseFloat(decompString[11]);
                        integrityCheck++;
                    }

                if (decompString[13] != null && decompString[14] != null)
                    if ( "Ho".equals(decompString[13]) ) {
                        temp.mHoistDriveHours = Float.parseFloat(decompString[14]);
                        integrityCheck++;
                    }

                if (decompString[16] != null && decompString[17] != null)
                    if ( "Tr".equals(decompString[16]) ) {
                        temp.mTrolleyDriveHours = Float.parseFloat(decompString[17]);
                        integrityCheck++;
                    }

                if (decompString[19] != null && decompString[20] != null)
                    if ( "Ga".equals(decompString[19]) ) {
                        temp.mGantryDriveHours = Float.parseFloat(decompString[20]);
                        integrityCheck++;
                    }

                if (decompString[22] != null && decompString[23] != null)
                    if ( "Ho+Tr".equals(decompString[22]) ) {
                        temp.mHoistPlusTrolleyHours = Float.parseFloat(decompString[23]);
                        integrityCheck++;
                    }

                if (decompString[25] != null && decompString[26] != null)
                    if ( "CC".equals(decompString[25]) ) {
                        temp.mContainerCounts = Integer.parseInt(decompString[26]);
                        integrityCheck++;
                    }

                if (decompString[28] != null)
                    if ( "Spr".equals(decompString[28]) ) {
                        //Spare
                        integrityCheck++;
                    }

                if ( integrityCheck > 6 ) {
                    //Successful or Partially successful Parsing
                    if ( decompString[1] != null ) {
                        craneIndex = Integer.parseInt(decompString[1]);
                        MainActivity.mCranesData.add(/*craneIndex,*/ temp);

                        synchronized(MainActivity.mExpectedDataFromEquipments) {
                            for ( int iter = 0; iter < MainActivity.mExpectedDataFromEquipments.size(); iter++ )
                                if ( craneIndex == MainActivity.mExpectedDataFromEquipments.get(iter) ) 
                                    MainActivity.mExpectedDataFromEquipments.set(iter, -1);
                        }
                    }
                    else
                        MainActivity.mCranesData.add(temp);
                }
                else {
                    //Failed Parsing
                    MainActivity.mErroneousCraneMessage.add(str);
                }
            }
        }
        else 
        {
            //DO SOME CLEAN UP
        }
    } catch (NumberFormatException ex) {
        System.out.println(ex.getMessage() );
        System.out.println("Exception SmsRceiver");
    } catch (NullPointerException ex) {
        System.out.println(ex.getMessage());
        System.out.println("Exception SmsRceiver");
    }
}

}

Safi
  • 21
  • 7
  • check this http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception – kalyan pvs Mar 27 '14 at 04:39
  • which line is giving null point exception – Shakeeb Ayaz Mar 27 '14 at 04:39
  • Logcat Output, please edit and upload –  Mar 27 '14 at 04:40
  • Based from personal experience, `ex.getMessage()` can return `null` without any info. You might want to check for `null` before you print the `Exception`, or just print the stack trace directly. – Andrew T. Mar 27 '14 at 04:40
  • Hi plz dont compare BroadcastReceiver class in android with the Broadcast Receivers in Radios.....No way noise or interference can come in your case... some logical or syntax error. – ASP Mar 27 '14 at 05:11
  • In the Catch block for NumberFormatException, the line System.out.println(ex.getMessage()); is the culprit. – Safi Mar 27 '14 at 05:21
  • No I actually mean the electrical interference between boards. its an amateurbuilt board that sends data to the GSM module via a very noisy connector. the board does not have a ground plane or any other sort of electrical shielding. so some characters can be erroneos symbols in some messages. for example 23456.92 can be turned into 23$56.92. – Safi Mar 27 '14 at 05:24
  • Post the logcat error message which shows the 'NullPointerException' – Ramakishna Balla Mar 27 '14 at 05:39
  • I resolved the issue by checking my arguements before I pass them to not calling getMessage anymore. I believe the the code was throwing a null and not just a NullPointerException. I could be wrong. – Safi Apr 08 '14 at 08:09

0 Answers0