异常为空

时间:2014-03-27 04:37:24

标签: android nullpointerexception numberformatexception

现在我知道这个问题已被问过几次,但我似乎无法找到具体的解决方案。在我看来,解决方法是针对每个案例的,所以我会在这里提出我的案例。

我正在扩展一个BroadcastReceiver,它从我的系统中捕获一些特定的消息并对其进行处理。现在我相信发送消息的电路容易产生很多噪声或干扰,这会导致消息出现一些错误,进而导致NumberFormatException。

然而,当我捕获异常并将其打印出来时,它又会生成NullPointerException。而且,对于我的生活,我无法弄清楚为什么会发生这种情况以及如何解决它。代码如下。

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");
    }
}

}

0 个答案:

没有答案