null bucket in NetworkStats.Bucket (querySummaryForDevice) Android

346 views Asked by At

This is my code

                Calendar startCalendar;
                Calendar stopCalendar;
            startCalendar= Calendar.getInstance();
            stopCalendar=Calendar.getInstance();
            startCalendar.set(2020,10,2,0,0);
            stopCalendar.set(2020,10,3,0,0);
            NetworkStatsManager networkStatsManager = (NetworkStatsManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE);
            NetworkStats.Bucket bucket;
                bucket = networkStatsManager.querySummaryForDevice(NetworkStats.Bucket.DEFAULT_NETWORK_ALL,"",startCalendar.getTimeInMillis(),stopCalendar.getTimeInMillis());
                long rxBytes = bucket.getRxBytes();
                long txBytes = bucket.getTxBytes();

I want to get the daily internet usage. The app has acess to data usage, also I checked the required permision in the manifest.

this is the log

2020-10-02 10:42:56.448 28091-28091/com.sreejithsreejayan.internetspeedometer D/AndroidRuntime: Shutting down VM
2020-10-02 10:42:56.452 28091-28091/com.sreejithsreejayan.internetspeedometer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sreejithsreejayan.internetspeedometer, PID: 28091
    java.lang.NullPointerException: Attempt to invoke virtual method 'long android.app.usage.NetworkStats$Bucket.getRxBytes()' on a null object reference
        at com.sreejithsreejayan.internetspeedometer.ui.home.HomeFragment$2.onClick(HomeFragment.java:71)
        at android.view.View.performClick(View.java:7870)
        at android.widget.TextView.performClick(TextView.java:14970)
        at android.view.View.performClickInternal(View.java:7839)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29363)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
2020-10-02 10:42:56.490 28091-28091/com.sreejithsreejayan.internetspeedometer I/Process: Sending signal. PID: 28091 SIG: 9

why is the bucket empty(null). I am new to android development please help.

1

There are 1 answers

0
Sreejith N S On

I replaced NetworkStats.Bucket.DEFAULT_NETWORK_ALL with ConnectivityManager.TYPE_WIFI then it wasnt null but ConnectivityManager.TYPE_WIFI is deprecated