Skip to content
Advertisement

Android app crashes with “Attempt to invoke virtual method on a null object reference”

I have a “Bottom navigation bar app” with 3 tabs or fragments. On the first fragment I have a PaintView that I can draw on. Drawing works great. But I’m trying to wire up a clear screen function that is invoked via an options menu dropdown. When I select the option to wipe the screen from the options menu, the app crashes with the following error:

JavaScript

Here is the code in the HomeFragment.java file for the options menu:

JavaScript

And here is the code in the PaintView file which contains the clearView() method that I am trying to invoke:

JavaScript

I’m thinking that the problem is due to the way that I instantiate the PaintView class in the HomeFragment.java file, but I don’t get any compilation errors and I can use dot notation paintView.clearView() to access the methods within the PaintView class just fine.

Any tips on what I am doing wrong would be greatly appreciated!

Here are a few screenshots:

First Tab: HomeFragment with Options menu

Options menu item with Clear Screen

Crash dialog after selecting Clear Screen

Advertisement

Answer

Try to use the variables, which you define:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement