Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:01:12+00:00 2026-06-05T23:01:12+00:00

When I send the SMS I am getting NullPointerException . I think may be

  • 0

When I send the SMS I am getting NullPointerException. I think may be issue in context. I am using this in TabActivityGroup.

This is my code:

   if(sendSMS.equals("1")) {
    double invoiceVal = getInvoiceValue();
    String phone = getPhoneNo(); 
    String[] arrPhone = phone.split(";");
    System.out.println("==arrPhone==" +arrPhone.length);
    StringBuffer sms = new StringBuffer();
    sms.append("Miscellaneous Sale");
    sms.append("\n");
    sms.append("\n");
    sms.append("Territory: " + territoryCode +"\n");
    sms.append("Sales Rep: " + strExecutive +"\n");
    sms.append("Route Code: " + routeCode +"\n");
    sms.append("Retailer Code: " + retailCode +" " +retailerName +"\n");
    sms.append("Invoice No: " + printInPrefix+"\\"+printInvoiceNo +"\n");
    sms.append("Invoice Value: " + df.format(invoiceVal) +"\n");
    sendSMS(arrPhone, sms.toString());
}



 String sendSMS(String[] arrPhone, String message){        
     String SENT = "SMS_SENT";
     smsStatus ="YES";
     String DELIVERED = "SMS_DELIVERED";
     String phoneNumber = "";
     try {
         for (int i = 0; i < arrPhone.length; i++) {
             phoneNumber = arrPhone[i];
             System.out.println("==phoneNumber==" +phoneNumber);
             PendingIntent sentPI = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(SENT), 0);
             PendingIntent deliveredPI = PendingIntent.getBroadcast(getBaseContext(), 0,new Intent(DELIVERED), 0);
             registerReceiver(new BroadcastReceiver(){
                @Override
                 public void onReceive(Context arg0, Intent arg1) {
                     switch (getResultCode()){
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                            smsStatus ="NO";
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service",  Toast.LENGTH_SHORT).show();
                            smsStatus ="NO";
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                            smsStatus ="NO";
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                            smsStatus ="NO"; 
                            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();
                                smsStatus ="NO";
                                break;                        
                        }
                    }
                }, new IntentFilter(DELIVERED));        

                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       
        }
    } catch (Exception e) {
        e.printStackTrace();
        smsStatus ="NO";
    }
     return smsStatus;

}   

Error:

     06-21 15:46:31.335: W/System.err(7510): java.lang.NullPointerException
06-21 15:46:31.345: W/System.err(7510):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:349)
06-21 15:46:31.345: W/System.err(7510):     at xont.ventura.controller.sales.LineDiscountActivity.sendSMS(LineDiscountActivity.java:1498)
06-21 15:46:31.345: W/System.err(7510):     at xont.ventura.controller.sales.LineDiscountActivity.saveInvoice(LineDiscountActivity.java:1354)
06-21 15:46:31.345: W/System.err(7510):     at xont.ventura.controller.sales.BatchActivity$2$1.onClick(BatchActivity.java:164)
06-21 15:46:31.345: W/System.err(7510):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:193)
06-21 15:46:31.345: W/System.err(7510):     at android.os.Handler.dispatchMessage(Handler.java:130)
06-21 15:46:31.355: W/System.err(7510):     at android.os.Looper.loop(Looper.java:154)
06-21 15:46:31.355: W/System.err(7510):     at android.app.ActivityThread.main(ActivityThread.java:4668)
06-21 15:46:31.355: W/System.err(7510):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 15:46:31.355: W/System.err(7510):     at java.lang.reflect.Method.invoke(Method.java:552)
06-21 15:46:31.355: W/System.err(7510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
06-21 15:46:31.355: W/System.err(7510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
06-21 15:46:31.355: W/System.err(7510):     at dalvik.system.NativeStart.main(Native Method)

I have tried using SalesActivityGroup.group.getApplicationContext() , SalesActivityGroup.group.getParent() , But till I’m getting same error.

NullPointerException line no 1498 place is: registerReceiver(new BroadcastReceiver(){

Please tell me what is an issue in my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:01:14+00:00Added an answer on June 5, 2026 at 11:01 pm

    Replace getBaseContext() with arg0

    You have errors here.

    onReceive gives you context in onReceive.Then you should not use getBaseContext()

    public void onReceive(Context arg0, Intent arg1) {   <--- Use this context i.e arg0
                         switch (getResultCode()){
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                                break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code to send sms throuh gsm modem for (int i =
I want to send an SMS via intent, but when I use this code,
I found free source project to send sms using java comm: http://code.google.com/p/find-ur-pal/source/browse/src/?r=21 Function sending
Working on a data collection application using mobile sms. when a customer send a
I am trying to access and send SMS through Wavecom's FXT-009 using Serial Port
I want to send / receive sms using AT commands from my computer to
Possible Duplicate: SMS from web application Sample code to send Mail via php //
Possible Duplicate: SMS from web application Sample code to send Mail via php //
i am building this sms notification system, which will send 10 times free sms
I am trying to write a small program using Qt to send SMS messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.