0

I am using a simple java library file for Undo and Redo text as shown in the tutorial and sample android app but for me when I run the app it shows me the following error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setEnabled(boolean)' on a null object reference
    at com.apps.primalnotes.Fragments.EditorFragment.textAction(EditorFragment.java:1023)
    at com.apps.primalnotes.Fragments.EditorFragment.onCreateView(EditorFragment.java:84)

Following is the library and method I am following on GitHub enter link description here

And exactly i am doing the following

 EditorFragment extends Pantalla  implements TextUndoRedo.TextChangeInfo, View.OnClickListener{
    private TextUndoRedo TUR;
    private ImageView undo, redo;

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.editor, container, false);

        getActivity().setTitle("Editor");

        setHasOptionsMenu(true);

        imm =(InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        base = new DataBase(context, null);


    text = (EditText) view.findViewById(R.id.texto);
    undo = (ImageView)view.findViewById(R.id.undo);
    redo = (ImageView)view.findViewById(R.id.redo);
    TUR = new TextUndoRedo(text, this);

            textAction(); "Showing error here"

            undo.setOnClickListener(this);
            redo.setOnClickListener(this);
    }

    @Override
        public void textAction() {
            undo.setEnabled(TUR.canUndo());  "Showing error here"
            redo.setEnabled(TUR.canRedo());
        }

    @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.undo:
                    TUR.exeUndo();
                    break;
                case R.id.redo:
                    TUR.exeRedo();
                    break;
S Shah
  • 428
  • 5
  • 15

3 Answers3

1

Your code doesn't have a "setContentView(R.layout.{file name of layout})".

Could you check it? It should be performed before using findViewById method.

Seungmin Ha
  • 151
  • 1
  • 5
  • I have that but just for simplicity, I edit my code here. If you can look at the tutorial link its simple there and I am doing the same. but getting this error in my case – S Shah Dec 03 '18 at 01:26
  • By the stack trace, the 'undo' variable is null. So I recommend check the layout file has ImageView with "@+id/undo" or not. – Seungmin Ha Dec 03 '18 at 01:29
  • It has nothing to do with ids. ids are fine – S Shah Dec 03 '18 at 01:45
0

Here the code from the Github source

btn_undo = (Button) findViewById(R.id.btn_undo);
btn_undo.setOnClickListener(this);
btn_redo = (Button) findViewById(R.id.btn_redo);
btn_redo.setOnClickListener(this);

And here the code from your code :

text = (EditText) view.findViewById(R.id.texto);
undo = (ImageView)view.findViewById(R.id.undo);
redo = (ImageView)view.findViewById(R.id.redo);
TUR = new TextUndoRedo(text, this);

The sample using Button for the undo/redo and you using ImageView. Check what is your ImageView ID in xml. Same with other here, your undo/redo ImageView is Null.

MrX
  • 953
  • 2
  • 16
  • 42
  • It has nothing to do with ImageView or Button. I tried button widget also but giving me the same error. – S Shah Dec 03 '18 at 01:38
  • The other possibility, are you sure that you are inflate the right xml layout. Null from widget cause the widget is not assign or its not on your right xml. – MrX Dec 03 '18 at 01:44
  • XML layout and ids are all fine. Error is just coming with the textAction(); call and the undo.setEnabled(TUR.canUndo()); "Showing error here" redo.setEnabled(TUR.canRedo()); – S Shah Dec 03 '18 at 01:45
  • Yes I know, but logically you can set `something` to A if the A is not in there. If you say you have right assign for the `ImageView` my last advice is handle it with `NullPointerException` – MrX Dec 03 '18 at 01:54
0

Check your XML. Your ImageView for undo & redo should have an id that looks like this,

android:id="@+id/undo

or

android:id="@+id/redo"
F4y5
  • 43
  • 10