Android 异常为空

Android 异常为空,android,nullpointerexception,numberformatexception,Android,Nullpointerexception,Numberformatexception,现在我知道这个问题已经被问了好几次,但我似乎找不到一个具体的解决办法。在我看来,解决办法是针对每个案例的,所以我会在这里提出我的案例 我正在扩展一个BroadcastReceiver,它从我的系统捕获一些特定的消息并对其进行处理。现在我相信发送信息的电路容易受到很多噪音或干扰,这会导致信息中出现一些错误,进而导致数字格式异常 但是,当我捕获异常并将其打印出来时,它又会生成一个NullPointerException。而我,就我的一生而言,无法理解为什么会发生这种情况,我该如何解决它。代码如下 p

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

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

但是,当我捕获异常并将其打印出来时,它又会生成一个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");
    }
}
公共类SmsReceiver扩展了BroadcastReceiver{
公共void onReceive(上下文、意图)
{
int-craneIndex=0;
整数整校验=0;
试一试{
Bundle=intent.getExtras();
String str=新字符串();
对象[]消息=(对象[])bundle.get(“PDU”);
SmsMessage[]sms=新SmsMessage[messages.length];
for(int i=0;i6){
//成功或部分成功解析
if(反编译字符串[1]!=null){
craneIndex=Integer.parseInt(反编译字符串[1]);
MainActivity.mCranesData.add(/*craneIndex,*/temp);
已同步(MainActivity.MeExpectedDatafromEquipments){
对于(int iter=0;iter