Issue
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);
}
}
Solution
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();
Answered By - NDY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.