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

  • SEARCH
  • Home
  • 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 8098265
In Process

The Archive Base Latest Questions

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

Hi I am currently learning how to use service and notifications in android and

  • 0

Hi I am currently learning how to use service and notifications in android and I am facing a NullPointerException when I am trying to start a service.
I wrote a small app where I have a button and 2 EditTextes (title and content). When I click on the button, I retrieve the values in the EditTextes and I start a new service that creates a notification. My code is splitted in two parts. Here is my code. This one presents the main activity of the application :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.serviceButton);

    final Context ctx = getApplicationContext();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent i = new Intent(ctx, NotificationService.class);


            Toast.makeText(ctx, "Clicking ...", Toast.LENGTH_SHORT).show();

            EditText title = (EditText) findViewById(R.id.notif_title);
            EditText content = (EditText) findViewById(R.id.notif_content);

            Bundle bundle = new Bundle();
            bundle.putCharSequence("NOTIF_TITLE", title.getText());
            bundle.putCharSequence("NOTIF_CONTENT", content.getText());
            i.putExtras(bundle);

            startService(i);

        }
    });
}

This one describes the service that will launch the notification :

public class NotificationService extends IntentService {

public NotificationService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    Context ctx = getApplicationContext();
    Toast.makeText(ctx, "NotificationService", Toast.LENGTH_LONG).show();

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);


    Bundle b = intent.getExtras();

    String title = b.getString("NOTIF_TITLE");
    String content = b.getString("NOTIF_CONTENT");
    Intent i = new Intent(this, NotificationService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i,
            Intent.FLAG_ACTIVITY_NEW_TASK);

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon,
            "New notification arrives", when);

    notification.setLatestEventInfo(ctx, title, content, pendingIntent);

    notificationManager.notify(
            NOTIFICATIONS.HELLO_NOTIFICATION.ordinal(), notification);
}

}

When I click on the button, I have this exception :

    FATAL EXCEPTION: main
 java.lang.NullPointerException 
at  android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:67)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:62)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Edit: I made correction to my code, removing new NotificationService but I still get a NullPointerException

  • 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-05T22:01:44+00:00Added an answer on June 5, 2026 at 10:01 pm

    You dont run your service by initiating its object as new. Simply run your service as below:

    startService(i);
    

    For more info, read this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning on how to use the Python optparse module. I'm trying the
Hey. I'm currently learning to use MySql and have a couple of questions. 1)
im currently learning jQuery and have ran into a problem. I am trying to
I am learning and designing a WCF service. I have picked to use Windows
I am currently learning the zsh and now I wanted to use strftime but
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
i am currently learning Zend Framework 1.10 and intend to use Doctrine 2. however
I am currently trying to create a REST web service for my django site
I'm currently learning to use MasterPages and ContentPlaceHolders in ASP.NET 3.5 and C# -

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.