-4

this is my code when i implement get item count 1 it shows non static method cannot be reference when i implement count 2 it show null pointer exception on return statement helpme to fix this

   final int speedScroll = 1000;
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            if(count == Adapter4.getItemCount2())
                count =0;
            if(count < Adapter4.getItemCount2()){
                recyclerView4.smoothScrollToPosition(++count);
                handler.postDelayed(this,speedScroll);
            }
        }
    };
    handler.postDelayed(runnable,speedScroll);
}


@Override
public int getItemCount() {
    return albumList.size();
}
public static int getItemCount2() {

    List<Album4> albumList=null;
        return albumList.size();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Selva
  • 475
  • 1
  • 4
  • 10
  • You are making it `null` and how can you get the `size` of null variables.. Try with something like `List albumList=new ArrayList<>()` which will return `0`; – Guruprasad J Rao Mar 24 '17 at 06:30
  • I'm assuming the variable `albumList` is global, so try to remove or atleast rename the variable with the same name in your `getItemCount2` method – Barak Mar 24 '17 at 06:32
  • There are two thing which may cause an issue for your error 1st is: @Override public int getItemCount() { return albumList.size(); } may be albumList is not yet initialized and this is causing null exception. 2nd is: public static int getItemCount2() { List albumList=null; return albumList.size(); } you initialise the list with null and then return size which will cause null exception. – Hardik Maru Mar 24 '17 at 07:22

1 Answers1

0

Try this.Assuming the variable albumList is global. and albumList is not null;

final int speedScroll = 1000;
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            int count = 0;
            @Override
            public void run() {
                if(count == albumList.size())
                    count =0;
                if(count < albumList.size()){
                    recyclerView4.smoothScrollToPosition(++count);
                    handler.postDelayed(this,speedScroll);
                }
            }
        };
        handler.postDelayed(runnable,speedScroll);
    }


    @Override
    public int getItemCount() {
        return albumList.size();
    }
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57