خرید بک لینک

Vote count: 0

I've build a singleton class to check if I am online before performing an exteal database upload.

To do so, this is the class:

public class InteetCoection
{
    private static InteetCoection instance = null;

    public static InteetCoection getInstance()
    {
        if(instance == null)
        {
            instance = new InteetCoection();
        }

        retu instance;
    }


    private InteetCoection()
    {

    }

    public boolean isOnline(Context context)
    {

        CoectivityManager coMgr = (CoectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = coMgr.getActiveNetworkInfo();
        retu (networkInfo != null && networkInfo.isCoected());

    }
}

Outside, on an Activity, I call it like so:

if(InteetCoection.getInstance().isOnline(this))
{
//I am online, so I upload to my exteal database
}
else
{
// I am not online
}

However, when it enters in getInstance() method, it evaluates instance == null ( which when I debug is true ) but, instead of entering in true branch and performing instance = new InteetCoection() it skips this branck and retus null, which causes a null object exception outside.

Why is this happening? How is possible that if instance == null is true it skips the branch for true case?

Thanks!

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 19 خرداد 1395 ساعت: 14:29

صفحه بندی