Chronometer null object reference

android chronometer format
chronometer android
how to reset chronometer in android
android chronometer milliseconds
android chronometer countdown
custom chronometer android
android chronometer hours minutes seconds
how to get chronometer value in android

I have a chronometer in second activity. I wanted to access it from main activity so i put the chronometer in second activity as public static and "secondActivity.chronometer" in main activity but when launching the app it shows chronometer.setbase(long) on a null object reference I've checked the xml id is correct. Does anyone know how to solve the problem?

you have not initialized the chronometer. when you launch the app , chronometer don't have any value because it is in second activity .

java, include setContentView(R.layout.main_layout); in your mainactivity otherwise findviewbyid will not be able to find chronometer and it will always show null� A null object is one that doesn't have any referenced value. The null object pattern is a behavioral design pattern that is used to provide a consistent return object in lieu of null. A NULL Object

When you launching the app, the second activity is not initialized. The chronometer , as a TextView's subclass, also cannot be initialized. It is not a good idea to access or change a view in other activity. You can transfer data and show by the data。

Chronometer, private static void setChronometer(Chronometer chronometer, int mode) { switch (mode) Get the chronometer reference Chronometer chronometer getStartTime() == null) { // If the start date is not defined, it's a new ViewModel so set it. long� How to start a Chronometer when a DialogFragment is clicked (its OK button)? I have something like the following, but it gives null object reference

As I mentioned in my comment, retaining a static reference to a View is a very bad idea. First, as other answers have made clear, there is not guarantee that your View will have been initialised when you call it, as the Activity it is part of may have not yet initialised. However, it also a prime cause of memory leaks (see this answer for more detail).

My recommendation would be that instead of starting your Chronometer, instead make a note of the time you want it to start (which, in the case of Chronometer, can be obtained by calling SystemClock#uptimeMillis), writing it to an Intent, and using that to launch your second Activity. You can then use this time as the base time for your Chronometer, and therefore give the impression that it has been running the entire time. I.e.

public class ActivityOne extends Activity {
    private void startChronometer() {
        final Intent i = new Intent(this, ActivityTwo.class);
        i.putExtra("time", SystemClock.uptimeMillis());
public class ActivityTwo extends Activity {

    private Chronometer chrono;  

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        chrono = findViewById(;
        chrono.setBase(getIntent().getLongExtra("time", SystemClock.uptimeMillis()));


Java Code Examples android.widget.Chronometer.setBase, Initialize this Chronometer object. Set the time that the count-up timer is in reference to. If the format string is null, or if you never call setFormat(), the. 3:27 That means that the location variable here is now null. 3:29 Now when we tried to get the X field from the location object, 3:35 it can't do what we asked, because it isn't the location. 3:38 This is null, and null is the absence of a location. 3:42 This causes the null reference exception to be thrown.

core/java/android/widget/, Chronometer.setBase(long)' on a null object reference at ActivityThread.performLaunchActivity( at� public final class Chronometer extends java.lang.Object This class implements a virtual digital stopwatch (or chronometer), providing basic and useful functionalities for time measurement, such as start() , stop() , multiple laps and total elapsed running time .

java, Object extended by java.lang.Enum<Chronometer.Precision> extended by oracle .javatools.util. Defines the method to be utilized by a Chronometer instance in order to obtain current time. NullPointerException - if the argument is null Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference Nullable reference types are available beginning with C# 8.0, in code that has opted in to a nullable aware context.Nullable reference types, the null static analysis warnings, and the null-forgiving operator are optional language features.

Chronometer.Precision (Oracle Fusion Middleware Java API , Object. This class implements a virtual digital stopwatch (or chronometer), providing basic and useful Parameters: label - Non-null label for the new lap� Here is the full error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at - 3879409

  • This is a terrible idea for several reasons, not least of all because you are almost certainly creating a memory leak by maintaining a static reference to a View (your Chronometer). Use an Intent or event bus or some other mechanism for communicating between your two Activities (see…)
  • cause i'm actually wanted to let my chronometer running in background when launching main activity using Service does it also have to use Intent to access the chronometer? won't it affect the Service?
  • By "in the background", I take it you mean it isn't visible? In that case, it doesn't need to be running - you just need to store the "start time" of your chronometer somewhere and then, when your chronometer is visible, call Chronometer#setBase using the time, and it will update to the correct difference between then and now. Have a look at my answer.
  • So I have to remove Intent as it just directly start the second activity instead of making it running in background which isn't visible?
  • Sure, you can invoke the Intent whenever you're ready to show the chronometer. Otherwise, you only need to store that "start time" somewhere. It could be in memory, or you can write it to SharedPreferences or something.