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

  • SEARCH
  • Home
  • 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 6363491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:02:43+00:00 2026-05-25T00:02:43+00:00

please explain me what’s wrong with following code. I am trying to detect if

  • 0

please explain me what’s wrong with following code. I am trying to detect if there is an Internet connection before launching an intent and otherwise display a toast stating that there is no Internet connection. It’s coming with force to close .

 case 7:   
                    final Activity ctx = null;
                    final ConnectivityManager connectManager = 
                           (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
                     final boolean connected = 
                           (connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                            connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI  ).getState() == NetworkInfo.State.CONNECTED   );
                    Intent intent7 = new Intent(v.getContext(),News.class);
                    if(connected ){  
                    startActivity(intent7);
                    }else{
                         Toast.makeText(Home.this, "No internet connection  " , Toast.LENGTH_SHORT).show();
                    }
                  break;  

Its coming with the following logcat

08-23 18:10:39.985: ERROR/AndroidRuntime(6555): Uncaught handler: thread main exiting due to uncaught exception
08-23 18:10:39.995: ERROR/AndroidRuntime(6555): java.lang.NullPointerException
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at Test.bed.Home$1.onItemClick(Home.java:73)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.widget.AbsListView.onKeyUp(AbsListView.java:1757)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.widget.GridView.commonKey(GridView.java:1470)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.widget.GridView.onKeyUp(GridView.java:1382)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.KeyEvent.dispatch(KeyEvent.java:1249)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.View.dispatchKeyEvent(View.java:3683)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:758)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:760)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1691)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1111)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.app.Activity.dispatchKeyEvent(Activity.java:2045)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1667)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2390)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2360)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.os.Looper.loop(Looper.java:123)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at android.app.ActivityThread.main(ActivityThread.java:4595)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-23 18:10:39.995: ERROR/AndroidRuntime(6555):     at dalvik.system.NativeStart.main(Native Method)
  • 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-25T00:02:43+00:00Added an answer on May 25, 2026 at 12:02 am

    You could also try this:

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
    

    But yeah, you’re getting a NullPointerException because the first variable is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please explain how the following snippet of code in C is a valid one
Please explain the following piece of code.. printf(%c\n,0+'0'); --> returns 0 printf(%c\n,1+'0'); --> returns
Please explain why the following pieces of code behave differently. #include<stdio.h> int main(){ float
Please Explain the following code #include <iostream> using namespace std; int main() { const
Please explain why the following code snippet fails to compile: public class ScjpTest{ static
Can someone please explain why the following code causes my app to bomb? NSData
Please explain to me why do the following code throw IllegalThreadStateException? try { if(thread1
Please explain this simple code: public int fibonacci(int n) { if(n == 0) return
Please explain me why that code: class kategoria { public $IdKat; public $NazwaKat; public
Please explain following IF statement (Microsoft Excel 2010). =IF(I12=0,Table!D13,Table!D15)+0.5 Please also explain the evaluation.

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.