i am getting java.lang.NullPointerException when trying to check internetConnection?

Nirthakali

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);

    }



}
NDY

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am i getting a NullPointerException when trying to save an entity to a list?

Why am i getting a nullpointerexception when I try to setAdapter?

Getting Error: java.lang.NullPointerException when work with Array in Java

Why am i getting Type errors when i am trying to check for collision and how can i fix it?

Why I am not getting NullPointerException?

I am trying to write a code but facing a java.lang.StringIndexOutOfBoundsException. Please check the code out

I am getting java.lang.NullPointerException on Jenkisn while calling method from groovy script

Why am I getting a java.lang.StringIndexOutOfBoundsException error when I run?

why I am getting : java.lang.NullPointerException?

I am getting NullpointerException at the time of pressing a button (on Search Button)java.lang.NullPointerException

I am getting error java.lang.NullPointerException when insert element in Vector

Why am I getting NullPointerException when running this code?

Why I am getting java.lang.NullPointerException?

java.lang.NullPointerException when I'm trying to make button invisible

java.lang.NullPointerException when trying to use jdbc

I am getting "java.lang.ArithmeticException: / by zero" when I run my program

Getting java.lang.NullPointerException when trying to use @FindBy in webDriver

Why am I getting a java.lang.NullPointerException error when I try to switch activities in studio?

Getting Java.Lang.NullPointerException when trying to open GPS Settings page using Xamarin

I am getting error while trying to check orientation of app?

Why am I getting a nullPointerException in Java?

Why am I getting this "java.lang.NullPointerException: Attempt to invoke virtual method '…' on a null object reference" error?

why am i getting a NullPointerException Java

Why am I getting a syntax error when I check my interface? Trying to make a MAC changer for Linux with Python/argparse

When I migrated to AndroidX I am getting java.lang.RuntimeException: Unable to start activity ComponentInfo

My app is crashing when i am trying to move from one activity to another activity due to java.lang.NullPointerException

i am getting a java.lang.NullPointerException while executing the testng script

I am getting a java.lang.NullPointerException when using Apache POI

I am getting a type error when I am trying to replace