user5465676
user5465676

Reputation:

Get NullPointerException when calling getSystemService()

My problem is, that in onCreate() method of my MainActivity I am creating new Thread object to which I want to pass reference to this activity, and than in that thread use it to call getSystemService(). But in the end, when I start the app it crashes and I get NullPointerException.

I have already found that problem could be that I am passing reference to activity befor super.onCreate(), but in my code super.onCreate() is performed before passing the reference.

This is my MainActivity's onCreate() method

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Instance which contains thread for obtaining wifi info
    final WifiInfoThread wifi_info = new WifiInfoThread(this);
....
}

And this is Thread class in which I am trying to get reference to system service

public class WifiInfoThread extends Thread {
// Constructor for passing context to this class to be able to access xml resources
Activity activity;
WifiInfoThread(Activity current) {
    activity = current;
}

// Flag for stopping thread
boolean flag = false;
// Obtain service and WifiManager object
WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);

// Runnable object passed to UIThread
Runnable uirunnable = new Runnable() {
    @Override
    public void run() {
        // Get current wifi status
        WifiInfo wifi_info = current_wifi.getConnectionInfo();

        // Things with showing it on screen
        TextView tv_output = (TextView) activity.findViewById(R.id.tv_output);
        String info = "SSID: " + wifi_info.getSSID();
        info += "\nSpeed: " + wifi_info.getLinkSpeed() + " Mbps";
        tv_output.setText(info);
    }
};

public void run() {
    flag = true;

    for(; flag; ) {
        activity.runOnUiThread(uirunnable);
        try {
            this.sleep(500);
        }
        catch(InterruptedException e) {}
    }
}

}

Upvotes: 3

Views: 2673

Answers (3)

iceman
iceman

Reputation: 826

The other answers show you how to fix this. You should also know what is the reason for the NullPointerException: In java your code does not get executed in the order you write it. Every thing written outside of member functions (methods) gets executed first (sort of). Then the constructor is called. Hence you are calling Conetxt.getSystemService() on activity, which is null.

Also for background work, android has AsyncTask and IntentService. Look them up.

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 83018

You are using activity.getSystemService before initializing activity. To get ride of this, move below line into Constructor

// Obtain service and WifiManager object
WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);

Like

WifiManager current_wifi;
WifiInfoThread(Activity current) {
    activity = current;
    current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);
}

Upvotes: 3

Blackbelt
Blackbelt

Reputation: 157467

move the initialitation current_wifi in the Constructor of your Thread.

// Obtain service and WifiManager object
WifiManager current_wifi = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);

in your case, activity is still a null reference. It gets a valid one after you assign it in the constructor

Upvotes: 1

Related Questions