null pointer exception on calling interface method implemented in other class

前端 未结 5 707
鱼传尺愫
鱼传尺愫 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.
        }
    }
    

提交回复
热议问题