0

I am Trying to loop through a list of numbers and send them a message but somehow my code is throwing a null pointer exception and i can't figure out where is the problem.

here the code

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MSGHandler {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    public BroadcastReceiver smsDeliveredReceiver,smsSentReceiver;


public MSGHandler(Context c,List<String> Name,List<String> Number,String Message)
{
    int Limit = Name.size();
    for(int i = 0; i < Limit;i++)
    {
        if(Number.get(i).length() > 0)
        {
            SmsManager sms = SmsManager.getDefault();
             PendingIntent piSent=PendingIntent.getBroadcast(c, 0, new Intent(SENT), 0);
                PendingIntent piDelivered=PendingIntent.getBroadcast(c, 0, new Intent(DELIVERED),0);

                //---when the SMS has been sent---
                smsSentReceiver = new BroadcastReceiver(){
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode())
                        {
                            case Activity.RESULT_OK:

                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:

                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:

                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:

                                break;
                        }
                    }
                };

                //---when the SMS has been delivered---
                smsDeliveredReceiver = new BroadcastReceiver(){
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode())
                        {
                            case Activity.RESULT_OK:

                                break;
                            case Activity.RESULT_CANCELED:

                                break;                        
                        }
                    }
                };
                c.registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
                c.registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
                sms.sendTextMessage(Number.get(i), null, Name.get(i)+Message, piSent, piDelivered);  

        }
    }
}
}

I am Calling this classes in an activity on a button click. Like this

Dialog Alert = new Dialog(MainActivity.this);
            try
            {
                MSGHandler sms = new MSGHandler(MainActivity.this,DBHandler.Contacts_Name_List,DBHandler.Contacts_Number_List,"HII!!!!");
            }catch(Exception e)
            {
                Alert.setTitle(e.toString());
            }
            Alert.show();

I am Using Eclipse and using my device as an emulator

here's log

 error opening trace file: No such file or directory (2)

  loaded /system/lib/egl/libEGL_mali.so

 loaded /system/lib/egl/libGLESv1_CM_mali.so

 loaded /system/lib/egl/libGLESv2_mali.so
  Enabling debug mode 0

(When the exception is thrown in the app .. log displays nothing )

Here are the ADb logs after i try to send the msg and the exception is thrown

enter image description here

Any Solutions?

user2801966
  • 418
  • 3
  • 11
  • Post the LogCat please. – Miguel Maciel Mar 21 '14 at 18:47
  • @MiguelMaciel i am not sure how to do that as i am testing it on my device – user2801966 Mar 21 '14 at 18:50
  • Which IDE are you using? Eclipse? IntelliJ? Android Studio? – Carl Anderson Mar 21 '14 at 18:57
  • @CarlAnderson Eclipse – user2801966 Mar 21 '14 at 18:59
  • Window > Show View > Other > LogCat – Cruceo Mar 21 '14 at 19:04
  • @Guardanis error opening trace file: No such file or directory (2) loaded /system/lib/egl/libEGL_mali.so loaded /system/lib/egl/libGLESv1_CM_mali.so loaded /system/lib/egl/libGLESv2_mali.so Enabling debug mode 0 – user2801966 Mar 21 '14 at 19:11
  • @MiguelMaciel error opening trace file: No such file or directory (2) loaded /system/lib/egl/libEGL_mali.so loaded /system/lib/egl/libGLESv1_CM_mali.so loaded /system/lib/egl/libGLESv2_mali.so Enabling debug mode 0 – user2801966 Mar 21 '14 at 19:12
  • @user2801966 Try connecting your device to your computer, check logcat for any messages to see if device is connected, when connected do your operation, then post the logs from logcat here. Other wise the probability of getting a good answer is pretty low – aravind Mar 21 '14 at 19:55
  • @aravind I am doing that only , when i run my program in eclipse it directly runs on my android device and all the log entries i have already posted ... when the app throws the exception the log doesn't add any entry however when i close my app on my device this entry add on the log "showStatusIcon on inactive InputConnection" which shows that my device is connected to the pc – user2801966 Mar 21 '14 at 19:59
  • ok, forget eclipse. Can you open adb logcat from your command line. You would need to open terminal, go to android-sdk_folder/platform-tools. Run `adb logcat` – aravind Mar 21 '14 at 20:02
  • @aravind Okie Aravind i edited the question and pasted the image of the log etries – user2801966 Mar 21 '14 at 20:12
  • Thanks. But one issue, can't see the exception! Check this image http://adrianvintu.com/blogengine/image.axd?picture=Colored+Logcat+Script+for+Windows+-+before+color.png. This is an example of an exception(Java) is thrown in adb if you let it crash – aravind Mar 21 '14 at 20:16
  • @aravind Done pasted the new log report .. not sure but i don't think i found any exception entry in the log – user2801966 Mar 21 '14 at 20:24
  • 1
    Sorry but still insufficient. You might be having this problem - http://stackoverflow.com/questions/11446049/error-opening-trace-file-no-such-file-or-directory-2. If not that, at least delete and create the virtual device, restart eclipse, adb kill-server, and try.. Without the error trace, cannot be of much help, sorry. – aravind Mar 21 '14 at 20:42
  • @aravind Okie No problem .. but still thumbs up for the help :) – user2801966 Mar 21 '14 at 20:45

1 Answers1

0

Well a nice sleep helped me alot and i found out that the exception was being thrown because i was trying to message on a blank number i.e " " a space

and the solution was using

 if(Number.get(i).trim().length() > 0)

instead of

 if(Number.get(i).length() > 0)
user2801966
  • 418
  • 3
  • 11