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 9219563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:08:41+00:00 2026-06-18T03:08:41+00:00

I have an activity which needs to perform an operation once a connection to

  • 0

I have an activity which needs to perform an operation once a connection to a network has been established. I tried implementing the Observer pattern but am getting a NullPointerException when I try to register the Observer activity to the subject. Any thoughts?

NetworkStatus

public class NetworkStatus implements NetworkStatusSubject, Runnable {

    List<NetworkObserver> observerList;
    Context context;
    public NetworkStatus(Context context) {
        this.context = context;
    }

    public static boolean isConnectedToInternet(Context context) {

        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

    /**
     * Add observer to list
     */
    @Override
    public void addObserver(NetworkObserver o) {
        observerList.add(o);

    }

    /**
     * Remove Observer from list
     */
    @Override
    public void removeObserver(NetworkObserver o) {
        observerList.remove(o);
    }

    @Override
    public void notifyObservers() {
        for(NetworkObserver networkObserver :  observerList) {
            networkObserver.updateStatus();
        }
    }

    @Override
    public void run() {

        while(true) {
            Log.d("NetworkStatus", "Checking Network Status");
            if(isConnectedToInternet(context)){
                notifyObservers();
                Log.d("NetworkStatus", "Network Connection is established");
            }
            else {
                Log.d("NetworkStatus", "Not connected to network");
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

Activity which needs notification

public class OfflineActivity extends Activity implements NetworkObserver {

    public GeoPoint ourLocationGeoPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.offline_mode_layout);

        //Register for network status updates
        NetworkStatus networkStatus = new NetworkStatus(this);
        networkStatus.addObserver(new OfflineActivity());
        networkStatus.run();

        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location
                // provider.
                /**
                 * Save Location to geopoint
                 */
                int lat = (int) (location.getLatitude() * 1E6);
                int lng = (int) (location.getLongitude() * 1E6);
                ourLocationGeoPoint = new GeoPoint(lat, lng);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };
        for (String provider : locationManager.getAllProviders()) {
            // Register the listener with the Location Manager to receive
            // location updates
            locationManager.requestLocationUpdates(provider, 0, 0,
                    locationListener);
        }
    }
    @Override
    public void updateStatus() {

        Log.d("OfflineActivity", "Connection Established, Uploading Points!");

    }
}
  • 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-06-18T03:08:42+00:00Added an answer on June 18, 2026 at 3:08 am

    observerList is declared as an instance variable but never instantiated.

    Instantiate it, the constructor is a good place for this:

    public NetworkStatus(Context context) {
            this.context = context;
            observerList = new List <NetworkObserver> ();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which needs to make two remote server calls. The first
I have a activity which has a button and 2 autocomplete widget. for the
Guys I have an activity which has a home button, when the button is
So I have an Activity (say TestActivity ) which needs to act as a
I have an Activity which has a listview. The listview is populated using an
I have an activity which needs to modify the SharedPreferences. public class AddingEmail extends
I have activity which needs to be active all the time. I have thread
I have a problem to test my non activity-class which need the context of
I have an activity which is going to switch around between several views, and
I have an Activity which shows a welcome message if started for the first

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.