0

I have some class application class extending Application. I use it as a controller for filling the database and perfoming REST calls. There is an interface:

private OnDBfilled onFilledListener;
...
public void setOnFilledListener(OnDBfilled onFilledListener) {
    this.onFilledListener = onFilledListener;
}
public interface OnDBfilled {
    void onFilledCallback();
}
...

When I've got required data call it to get known the Fragment, that data was successfully load from the internet and filled to database:

@Override
        public void onResponse(Call call, Response response) throws IOException {
...//working with data
onFilledListener.onFilledCallback();
}

For the first time, when the call is done and database is filled I can see the result in the log:

private Application controller;
private Application.OnDBfilled listener;
...
controller = new Application();
listener = new Application.OnDBfilled() {
        @Override
        public void onFilledCallback() {
            System.out.println("bd is filled. replacing fragments initiated editianal");
            replaceFragment();
        }
    };
 controller.setOnFilledListener(listener);

This works fine and I can see the logging message. But when I'm trying to do in the another fragment, I've got Null Pointer Exception in this line:

 onFilledListener.onFilledCallback();

Here's code of another fragment, but it's almost equal:

  private Application controller;
private Application.OnDBfilled filledDBListener;

...
//onCreateView
controller = new Application();
    filledDBListener = new Application.OnDBfilled() {
        @Override
        public void onFilledCallback() {
            swipeRefreshLayout.setRefreshing(false);
           // fillEventListFromBD(UserData.getCity(getActivity()));
        }
    };
    controller.setOnFilledListener(filledDBListener);

Any suggestions? Thanks!

Sigmund
  • 748
  • 1
  • 8
  • 28

1 Answers1

1

OnDBfilled flid static

 private static OnDBfilled onFilledListener; 

or

fragment & viewpager use:

 viewpager.setoffscreenpagelimit(9)
loadjang
  • 327
  • 1
  • 3
  • Did you even read the question? Quoted from the question: 'I've got Null Pointer Exception in this line: `onFilledListener.onFilledCallback();`' – 0xDEADC0DE Jul 05 '16 at 11:55