Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi
Sir , I am new on Android platform and I have written a separate class for Sending SMS with confirmation code but when I send SMS through this then I face a problem that Your application has closed unexpectedly due to Null Pointer exception Actually I have send the sms from SettingScreen Activity through send_SMS method defined here sir pl help me I bind that code with the query in this regards

thanks
Om Parkash Kaushik
Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.app.PendingIntent;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.app.Activity;
public class SendSMS extends Activity {
	private Context ctx;
	public SendSMS(Context object){
		this.ctx = object;
		}
	  //---sends an SMS message to another device---
	public void send_SMS(String phoneNumber, String message)
    { 
		try{
        String SENT = "SMS_SENT";
       // String DELIVERED = "SMS_DELIVERED";
 
       PendingIntent sentPI = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, new Intent(SENT),0);
    		   //getBroadcast( ctx, 0, new Intent(ctx,ctx.getClass()), 0);
      // PendingIntent deliveredPI = PendingIntent.getBroadcast( ctx, 0, new Intent(DELIVERED), 0);
 
       /// ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    	Toast.makeText(getBaseContext(), "SMS_SNT", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));
// 
//        //---when the SMS has been delivered---
//        registerReceiver(new BroadcastReceiver(){
//            @Override
//            public void onReceive(Context arg0, Intent arg1) {
//                switch (getResultCode())
//                {
//                    case Activity.RESULT_OK:
//                        Toast.makeText(getBaseContext(), "SMS delivered", 
//                                Toast.LENGTH_SHORT).show();
//                        break;
//                    case Activity.RESULT_CANCELED:
//                        Toast.makeText(getBaseContext(), "SMS not delivered", 
//                                Toast.LENGTH_SHORT).show();
//                        break;                        
//                }
//            }
//        }, new IntentFilter(DELIVERED));        
// 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, null);        
  
    }catch(Exception e){
    	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    }
	

}
Posted
Updated 24-May-23 5:22am
v2

1 solution

Change the following line...

catch(Exception e)

To

catch(Throwable e)

Once you do that you will get an exception (presuming the code you have there works.)
 
Share this answer
 
v2
Comments
Deepa Atiwadkar 22-Sep-22 23:10pm    
Install code problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900