Vlad Pintea Vlad Pintea - 2 months ago 3x
Java Question

Absolutely no idea how to resolve this NullPointerException

Yes, I've read this and this, but to no success. Here's what I'm trying to do:

I've created this application, which, after the user enters a number of seconds and presses a button, a service starts, which turns off the screen, starts a timer (based on the number of seconds inputted) and stops only when said timer ends. After stopping, the screen turns on and a TextView gets changed. Now, the idea is that, if the user turns on the screen before the timer ends, the service stops and the TextView doesn't get changed.

My issue comes when, after manually turning on the screen, if you enter the application and kill it, I receive the following error:

java.lang.RuntimeException: Unable to start service com.example.vladpintea.friendsbeforecents.MainService@1ab5327c with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

Said error points to this line of code (from MainService):

usedTimer = intent.getStringExtra("timer");

After reading the above links, I tried implementing a try-catch around the "usedTimer", but it didn't change anything. I also tried initializing "usedTimer" with " " (blank), but, again, no success.


public class MainService extends Service {

static String BROADCAST_ACTION = "com.example.vladpintea.friendsbeforecents.displayevent";
Handler handler = new Handler();
int counterr = 0;
Intent intentt;

String usedTimer;
long interval;

//TimerTask that will cause the run() runnable to happen.
TimerTask myTask = new TimerTask() {
public void run() { stopSelf(); }
//Timer that will make the runnable run.
Timer myTimer = new Timer();

public void onCreate() {
Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();

intentt = new Intent(BROADCAST_ACTION);

registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));

private BroadcastReceiver counter = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {

Toast.makeText(MainService.this, "Whoops! You've Lost.", Toast.LENGTH_SHORT).show();

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);

public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show();

usedTimer = intent.getStringExtra("timer");
try {
interval = Long.parseLong(usedTimer);
} catch (NumberFormatException ignored) {}

myTimer.schedule(myTask, interval);

handler.postDelayed(sendUpdatesToUI, 1000);

return super.onStartCommand(intent, flags, startId);

private Runnable sendUpdatesToUI = new Runnable() {
public void run() { handler.postDelayed(this, 1000); }

public void DisplayLoggingInfoPlus() {
intentt.putExtra("counter", String.valueOf(++counterr));

public void onDestroy() {

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
if ((wakeLock != null) && (!wakeLock.isHeld())) { wakeLock.acquire(); }

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 30000);


public IBinder onBind(Intent intent) { return null; }


public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

final Button startApp = (Button) findViewById(R.id.startApp);
final EditText timer = (EditText) findViewById(R.id.insertTimer);

assert startApp != null;
startApp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();

Intent startMain = new Intent(Intent.ACTION_MAIN);

Intent intent = new Intent(MainActivity.this, MainService.class);
assert timer != null;
intent.putExtra("timer", timer.getText().toString());

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION));

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) { updateUI(intent); }

private void updateUI(Intent intent) {
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
String counter = intent.getStringExtra("counter");

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();

assert txtCounter != null;

public void onDestroy() {
try { unregisterReceiver(broadcastReceiver); } catch (IllegalArgumentException ignored) {}



it's getting null value and you are trying to parse that null value so that it's throwing null pointer exception. you can use this code to handle this exception;

      return -1;
        return -1;

        usedTimer = intent.getStringExtra("timer");