0

I am writing simple code for enable the wifi.The code is displaying NUllPointerExcepation here. Can any one help me out in this.These code was working earlier but now it is not. Not sure what mistake i am doing.

Here is code :

TurnOnWIfiTestAction class :

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class TurnOnWIfiTestAction {

    private  WifiManager wifi_manager;
    private  WifiConfiguration wifi_config;
    private Context paramContext;

    public TurnOnWIfiTestAction() {

    }

    public void turnOnWifi() throws Exception {
         wifi_manager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);
         wifi_manager.setWifiEnabled(true);
         Thread.sleep(2000L);

    }

}

MainActivity

import com.wifiTest.TurnOnWIfiTestAction;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void wifienable (View view)
    {
        TurnOnWIfiTestAction wifinOn = new TurnOnWIfiTestAction();

        try {
            wifinOn.turnOnWifi();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Permissions :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

Logcate :

01-01 22:53:40.994: W/System.err(13438): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
01-01 22:53:40.994: W/System.err(13438):    at com.wifiTest.TurnOnWIfiTestAction.turnOnWifi(TurnOnWIfiTestAction.java:18)
01-01 22:53:40.994: W/System.err(13438):    at com.post.l_upgrade.MainActivity.wifienable(MainActivity.java:46)
01-01 22:53:40.994: W/System.err(13438):    at java.lang.reflect.Method.invoke(Native Method)
01-01 22:53:40.994: W/System.err(13438):    at java.lang.reflect.Method.invoke(Method.java:372)
01-01 22:53:40.994: W/System.err(13438):    at android.view.View$1.onClick(View.java:3986)
01-01 22:53:41.000: W/System.err(13438):    at android.view.View.performClick(View.java:4728)
01-01 22:53:41.000: W/System.err(13438):    at android.view.View$PerformClick.run(View.java:19508)
01-01 22:53:41.000: W/System.err(13438):    at android.os.Handler.handleCallback(Handler.java:739)
01-01 22:53:41.002: W/System.err(13438):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-01 22:53:41.002: W/System.err(13438):    at android.os.Looper.loop(Looper.java:135)
01-01 22:53:41.004: W/System.err(13438):    at android.app.ActivityThread.main(ActivityThread.java:5212)
01-01 22:53:41.004: W/System.err(13438):    at java.lang.reflect.Method.invoke(Native Method)
01-01 22:53:41.004: W/System.err(13438):    at java.lang.reflect.Method.invoke(Method.java:372)
01-01 22:53:41.004: W/System.err(13438):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
01-01 22:53:41.006: W/System.err(13438):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:686)
01-01 22:53:41.186: E/AudioPolicyManager(223): unknown stream type
user59053
  • 41
  • 2
  • 5
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Sep 18 '14 at 12:51

2 Answers2

3

Your paramContext=null at

 wifi_manager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);

So, you got NPE.

Corrected:

Pass context in Constructor and initialized it like below

 public TurnOnWIfiTestAction(Context con) {
   this.paramContext=con;
  }
M D
  • 47,665
  • 9
  • 93
  • 114
  • If you make `static` method, you don't need to create object. So give you suggestion to pass `context` directly in Method. – Pratik Butani Sep 18 '14 at 12:56
0

Pass context in turnOnWifi() like turnOnWifi(context) from MainActivity

Update:

public void turnOnWifi(Context context) throws Exception {
     wifi_manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     wifi_manager.setWifiEnabled(true);
     Thread.sleep(2000L);
}

Note: If you make static method, you don't need to create object of Class. So you can directly call like

TurnOnWIfiTestAction.turnOnWifi(context);

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • If i want to add more method and if every method need to access Context then in that case you should pass context as an single argument constructor. – M D Sep 18 '14 at 13:03
  • Currently He has only one method so it would be better. – Pratik Butani Sep 18 '14 at 13:04