1

The problem is NullPointerException in notification. Broadcosting receiver class call in Sms_Service:

public static void setMessage(long paramLong)
{        
    myContext.stopService(new Intent(myContext, Sms_Service.class));
    intent = new Intent(myContext, Sms_Service.class);
    myContext.startService(intent);
    ((AlarmManager)myContext.getSystemService("alarm")).set(0,   paramLongPendingIntent.getService(myContext, 0, intent, 0));                   
}

Sms_Service class:

public class Sms_Service extends Service
{
   public static long localObject;
   private MyCounter Timer;
   private FakeYouSharedPreference myBalanceData;
   private DataBaseAdapter myDB;
   private MessageBean mySmsData;
   private Vibrator phoneVibrate;
   private MediaPlayer ringtonePlay;
   private String uri;

public void createNotification()
  {
    int id= R.drawable.ic_sms;
    //SmsManager sm = SmsManager.getDefault();


    NotificationManager localNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    int notifyID = 1;
     android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
                System.currentTimeMillis());
    Cursor localCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "_id DESC");
    Uri localUri = null;
    if (localCursor.moveToFirst())
    {
      String str = localCursor.getString(localCursor.getColumnIndex("thread_id"));
      Log.d("fakeyou", str);
      localUri = Uri.parse("content://mms-sms/conversations/" + str);
    }
    Intent localIntent = new Intent("android.intent.action.VIEW", localUri);
    localIntent.addCategory("android.intent.category.DEFAULT");
    PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, localIntent, 0);
    notification.setLatestEventInfo(this, this.mySmsData.getNumber(), this.mySmsData.getBody(), localPendingIntent);
    notification.flags = (0x10 | notification.flags);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    localNotificationManager.notify(notifyID, notification);
  }
@Override
  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

 @Override
  public void onCreate()
  {
     super.onCreate();
    this.createNotification();

  }

  private class MyCounter extends CountDownTimer
  {

    public MyCounter(long arg2,long arg4)
    {
      super(localObject, arg2);
    }

    public void onFinish()
    {
      if (Sms_Service.this.ringtonePlay != null)
      Sms_Service.this.ringtonePlay.stop();
      Sms_Service.this.stopSelf();
    }

    public void onTick(long paramLong)
    {
    }
  }
}

Error is:

    09-10 08:53:46.862: E/AndroidRuntime(2653): FATAL EXCEPTION: main
    09-10 08:53:46.862: E/AndroidRuntime(2653): java.lang.RuntimeException: Unable to       create service com.fakeyou.Sms_Service: java.lang.NullPointerException
   java.lang.NullPointerException
    09-10 08:53:46.862: E/AndroidRuntime(2653):     at         Sms_Service.createNotification(Sms_Service.java:42)
    09-10 08:53:46.862: E/AndroidRuntime(2653):     at com.fakeyou.Sms_Service.onCreate(Sms_Service.java:70)
    09-10 08:53:46.862: E/AndroidRuntime(2653):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2529)
    09-10 08:53:46.862: E/AndroidRuntime(2653):     ... 10 more
4
  • Please mark line number 42 in Sms_service.java. Commented Sep 10, 2013 at 9:25
  • mySmsData is null because you never initialize it. Commented Sep 10, 2013 at 9:25
  • 1
    duplicate of stackoverflow.com/questions/18700819/… Commented Sep 10, 2013 at 9:25
  • LINE NUMBER 42 IS THIS android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(), System.currentTimeMillis()); Commented Sep 10, 2013 at 9:30

2 Answers 2

2

In line 42 of Sms_service.java:

android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
            System.currentTimeMillis());

mySmsData is null. You have to initialize this variable.

Sign up to request clarification or add additional context in comments.

1 Comment

is all ready initialize my code mySmsData( private MessageBean mySmsData) messageBean.class (get,set)
0

Please show the part of the Code where you initialized mySmsData. If you really initialized mySmsData, maybe you got the wrong line.

localCursor.moveToFirst() will also throw a NullPointerException if something goes wrong querying the ContentProvider:

Cursor localCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "_id DESC");

2 Comments

public String getNumber() { return this.number; } public String getBody() { return this.body; }
These are just the Methods of the MessageBean Object... but you don't instantiate the Object! In "onCreate" you have to do something like: this.mySmsData = new MessageBean(); and maybe call this.mySmsData.setNumber("1") or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.