null pointer exception on calling interface method implemented in other class

前端 未结 5 679
鱼传尺愫
鱼传尺愫 2020-12-21 21:59

I am trying to call the method getFailureDialog() of the interface OnSelectedListener. The method is implemented in MainActivity.java.

相关标签:
5条回答
  • 2020-12-21 22:37
    OnSelectedListener mCallback;
    

    is never getting initialized, or is being initialized with a null value.

    public class Common
    {
        OnSelectedListener mCallback = new OnSelectedListener(){
            public void getFailureDialog(){
                JOptionPane.showMessageDialog(null, "An Error Has Occurred.");
            }
        };
    
    
        public interface OnSelectedListener 
        {
            public void getFailureDialog();
        }
    
        public void myRecord(String email)
        {
            mCallback.getFailureDialog();  //now this works.
        }
    }
    
    0 讨论(0)
  • 2020-12-21 22:45

    You have to redesign your code man, make Common class implement OnSelectedListener interface. So separate OnSelectedListener as outer interface not as inner interface.

    i will code it like this.

    public interface OnSelectedListener 
    {
        public void getFailureDialog();
    }
    

    then Common class should be like this

    public class Common implements OnSelectedListener
    {
        public void getFailureDialog()
        {
            RecordFailure fd = new RecordFailure(); 
            fd.show(getSupportFragmentManager(), "dialog");
        }
    
        public void myRecord(String email)
        {
            getFailureDialog();
            //do something more rather than just call existing method
        }
    }
    

    and this code will run smoothly. if you need to implement the getFailureDialog in MainActivity, make this Common class as abstract class.

    Common class will be like this

    public abstract class Common implements OnSelectedListener
    {
        public abstract void getFailureDialog();
    
        public void myRecord(String email)
        {
            getFailureDialog();
            //do something more rather than just call existing method
        }
    }
    

    now your MainActivity class can extend from this class to implement the missing part.

    0 讨论(0)
  • 2020-12-21 22:47

    here is the source code for the OnSelectedListener. Since that is an interface, you have to initialize it using new and overriding the onSelected() method OR let your class implement this listener

    0 讨论(0)
  • 2020-12-21 22:49

    You need to modify Common and MainActivity. In Common add a basic constructor. Then activate the callback as shown in startMyCallback.

    Common.java

    public class Common
    {
    
     public Common() {}
    
     OnSelectedListener mCallback;
    
    
     public interface OnSelectedListener 
     {
        public void getFailureDialog();
     }
    
     public void myRecord(String email)
     {
        mCallback.getFailureDialog();  //null pointer exception here
     }
    }
    

    MainActivity.java

    public class MainActivity implements Common.OnSelectedListener
    {
    
        Common common = new Common();
    
        public MainActivity()
        {
    
        }
    
        public void startMyCallback()
        {
            common.mCallback = this;
        }
    
        @Override
        public void getFailureDialog()
        {
            RecordFailure fd = new RecordFailure(); 
            fd.show(getSupportFragmentManager(), "dialog");
        }
    }
    
    0 讨论(0)
  • 2020-12-21 22:51

    add a method in

    public class Common
    {
        OnSelectedListener mCallback;
    
        public void setOnSelectedListener(OnSelectedListener listener){
            mCallback = listener;
        }
    
        public interface OnSelectedListener 
        {
            public void getFailureDialog();
        }
    
        public void myRecord(String email)
        {
            mCallback.getFailureDialog();  //null pointer exception here
        }
    }
    

    now use the setOnSelectedListener() to initialize your listener

    but from you code you might need to implement another listener in your SharedFragment too.

    0 讨论(0)
提交回复
热议问题