0

this is the error i am getting

03-25 12:11:15.019  21907-21907/np.com.tester.wlms E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: np.com.tester.wlms, PID: 21907
        java.lang.RuntimeException: Unable to start activity ComponentInfo{np.com.tester.wlms/np.com.tester.wlms.MainActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5137)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at np.com.tester.wlms.MainActivity.onCreate(MainActivity.java:15)
                at android.app.Activity.performCreate(Activity.java:5231)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5137)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
                at dalvik.system.NativeStart.main(Native Method)

this is my internetConnectionCheck.java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.ActionBarActivity;


public class InternetConnectionCheck extends ActionBarActivity{
    public boolean isInternetOn()   //checking internet for connection
    {
        ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = con.getActiveNetworkInfo();
        // ARE WE CONNECTED TO THE NET
        if (networkInfo != null) {
            // MESSAGE TO SCREEN FOR TESTING (IF REQ)
            //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
            return true;
        } else {
            return false;
        }

    }
}

this is the MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
     private InternetConnectionCheck internetConnectionCheck;
     private  String internetCheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        internetCheck=String.valueOf(internetConnectionCheck.isInternetOn());

         Toast.makeText(this,internetCheck, Toast.LENGTH_SHORT);

    }



}
Pararth
  • 8,114
  • 4
  • 34
  • 51
Nirthakali
  • 45
  • 8

4 Answers4

1

You must create instance of InternetConnectionCheck .

Otherwise isInternetOn must be static method

public statis boolean isInternetOn()
{

}
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
1
public class Utility {
public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}


//Use it in your activity
boolean connected = Utility.isNetworkAvailable(this);
0

Problem is that InternetConnectionCheck is not initialize that why you are getting null exception.

Try this

internetConnectionCheck.java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.ActionBarActivity;


public class InternetConnectionCheck extends ActionBarActivity{
    public static  boolean isInternetOn()   //checking internet for connection
    {
        ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = con.getActiveNetworkInfo();
        // ARE WE CONNECTED TO THE NET
        if (networkInfo != null) {
            // MESSAGE TO SCREEN FOR TESTING (IF REQ)
            //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
            return true;
        } else {
            return false;
        }
 }
 }

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
     private InternetConnectionCheck internetConnectionCheck;
     private  String internetCheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        internetCheck=String.valueOf(InternetConnectionCheck.isInternetOn());

         Toast.makeText(this,internetCheck, Toast.LENGTH_SHORT);

    }



}
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
0

The problem is here:

 private InternetConnectionCheck internetConnectionCheck;

in connection with the call here

internetConnectionCheck.isInternetOn()

InternetConnectionCheck is your object and you define the variable name internetConnectionCheck for it. But you never ever initialize the object with something. Therefore its just null. As soon as you call the method .isInternetOn() you will always receive the NullPointerException. If you want to fix this, you need to create an instance of your object.

private InternetConnectionCheck internetConnectionCheck = new InternetConnectionCheck();
NDY
  • 3,527
  • 4
  • 48
  • 63