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