0

I am trying to find the signal strength of Wifi but I m getting a null pointer exception. While fetching the network informatiopns like SSID etc. Can anyone suggest me a solution how to remove the null pointer exception.

enter code here:

public class MyReciever extends BroadcastReceiver{
    WifiManager wifi;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        List<ScanResult> results=wifi.getScanResults();
        ScanResult bestSignal=null;

        for(ScanResult result:results)
        {
            if(bestSignal==null || WifiManager.compareSignalLevel(bestSignal.level, result.level)<0)
                bestSignal=result;
        }

        String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID);
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();

        Log.d("Debug","onRecieve() message:" +message);
    }

}



public class MainActivity extends Activity {

    TextView textStatus;
    WifiManager wifi;
    BroadcastReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textStatus=(TextView)findViewById(R.id.textStatus);

        wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);

        WifiInfo info=wifi.getConnectionInfo();
        textStatus.append("\n\nWifi Status: " +info.toString());

        List<WifiConfiguration> configs=wifi.getConfiguredNetworks();
        for(WifiConfiguration config:configs)
        {
            textStatus.append("\n\n" +config.toString());
        }

        if(receiver==null)
                receiver = new MyReciever();

        registerReceiver(receiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        Log.d("TAG", "onCreate()");
    }

    @Override
    public void onStop() {
        unregisterReceiver(receiver);
            super.onStop();
    }
    }
3
  • 6
    1. See the exception stacktrace in logcat. 2. Find the corresponding code line. 3. Look up the line in your code and figure out what is the null that is dereferenced there. 4. Edit the question to contain the additional information at least from steps 1 and 2. Commented Oct 21, 2013 at 10:20
  • simple surround with try..catch(){} block. Commented Oct 21, 2013 at 10:23
  • 10-21 06:23:52.851: E/AndroidRuntime(1045): FATAL EXCEPTION: main 10-21 06:23:52.851: E/AndroidRuntime(1045): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifi/com.example.wifi.MainActivity}: java.lang.NullPointerException 10-21 06:23:52.851: E/AndroidRuntime(1045): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 10-21 06:23:52.851: E/AndroidRuntime(1045): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) Commented Oct 21, 2013 at 10:28

1 Answer 1

1

Problem could be at String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID);

When there is no 'bestSignal' found, variable 'bestSignal' will be null and you are trying to execute bestSignal.SSID which might cause NPE.

Change you code like if (bestSignal != null) { String message=String.format("%s networks found. %s is the strongest", results.size(),bestSignal.SSID); }

Hope it helps :)

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

Comments

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.