Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:42:00+00:00 2026-05-27T14:42:00+00:00

I am creating a class to check internet connectivity, I am getting below error

  • 0

I am creating a class to check internet connectivity, I am getting below error :

 12-16 10:34:07.340: E/AndroidRuntime(1099):

java.lang.NullPointerException 12-16 10:34:07.340:
E/AndroidRuntime(1099): at
com.internetchecker.main.InternetCheckerActivity$1.onClick(InternetCheckerActivity.java:25)
12-16 10:34:07.340: E/AndroidRuntime(1099): at
android.view.View.performClick(View.java:2485) 12-16 10:34:07.340:
E/AndroidRuntime(1099): at
android.view.View$PerformClick.run(View.java:9080) 12-16 10:34:07.340:
E/AndroidRuntime(1099): at
android.os.Handler.handleCallback(Handler.java:587) 12-16
10:34:07.340: E/AndroidRuntime(1099): at
android.os.Handler.dispatchMessage(Handler.java:92) 12-16
10:34:07.340: E/AndroidRuntime(1099): at
android.os.Looper.loop(Looper.java:123) 12-16 10:34:07.340:
E/AndroidRuntime(1099): at
android.app.ActivityThread.main(ActivityThread.java:3683) 12-16
10:34:07.340: E/AndroidRuntime(1099): at
java.lang.reflect.Method.invokeNative(Native Method) 12-16
10:34:07.340: E/AndroidRuntime(1099): at
java.lang.reflect.Method.invoke(Method.java:507) 12-16 10:34:07.340:
E/AndroidRuntime(1099): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-16 10:34:07.340: E/AndroidRuntime(1099): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-16
10:34:07.340: E/AndroidRuntime(1099): at
dalvik.system.NativeStart.main(Native Method)

The code of my Class is below :

public class CheckInternet2 {
private static Boolean status =  true;
public static Boolean isConnected() {
    Runnable runnable = new Runnable() {
        public void run() {
            // TODO Auto-generated method stub
            isNetworkAvailable(h,2000);             
        }           
    };
    runnable.run();
    return status;
}
private static Handler h = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        if (msg.what != 1) { // code if not connected
            status = false;
            System.out.println("Status False");
        } else { // code if connected
            status = true;
            System.out.println("Status True");
        }
    }
};

private static  void isNetworkAvailable(final Handler handler, final int timeout) {

    new Thread() {

        private boolean responded = false;
        @Override
        public void run() {

            new Thread() {
                @Override
                public void run() {
                    HttpGet requestForTest = new HttpGet("http://m.google.com");
                    try {
                        new DefaultHttpClient().execute(requestForTest); // can last...
                        responded = true;
                    } catch (Exception e) {}
                }
            }.start();
            try {
                int waited = 0;
                while(!responded && (waited < timeout)) {
                    sleep(100);
                    if(!responded ) { 
                        waited += 100;
                    }
                }
            } 
            catch(InterruptedException e) {} // do nothing 
            finally { 
                if (!responded) { handler.sendEmptyMessage(0);
                } 
                else { handler.sendEmptyMessage(1); 
                }
            }
        }
    }.start();
}
}

and I am calling it as :

btnCheckStatus.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if(CheckInternet2.isConnected())
                {
                    Toast.makeText(getApplicationContext(), "Connected ", 0).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Not Connected ", 0).show();
                }


            }
        });

I am not able to figure out why its throwing exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:42:01+00:00Added an answer on May 27, 2026 at 2:42 pm

    check for the network availability along with wifi status

    To Check Network Availability

    public static boolean isNetworkPresent(Context context) {
            boolean isNetworkAvailable = false;
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            try {
    
                if (cm != null) {
                    NetworkInfo netInfo = cm.getActiveNetworkInfo();
                    if (netInfo != null) {
                        isNetworkAvailable = netInfo.isConnectedOrConnecting();
                    }
                }
            } catch (Exception ex) {
                Log.e("Network Avail Error", ex.getMessage());
            }
            //check for wifi also
            if(!isNetworkAvailable){
                WifiManager connec = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
                State wifi = cm.getNetworkInfo(1).getState();
                if (connec.isWifiEnabled()
                        && wifi.toString().equalsIgnoreCase("CONNECTED")) {
                    isNetworkAvailable = true;
                } else {
    
                    isNetworkAvailable = false;
                }
    
            }
            return isNetworkAvailable;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I'm creating a session class, with relevant implementation as below: public class Session()
I want to check if a class is a subclass of another without creating
When creating a class library in C++, you can choose between dynamic ( .dll
When creating a class that has internal private methods, usually to reduce code duplication,
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
I am creating a class that determines which of number of registered WCF client
I'm creating a class. What's the best practice for naming properties/methods when your preferred
I'm creating a class (say, C) that associates data (say, D) with an object
I am creating a class for an application backbone and I need this function
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.