I'm doing Google's android courses on Udacity to learn Android and Java. Currently, the project I'm working on is building an app which represents ordering coffee. There's a quantity TextView, along with a + and - button which on click either increment or decrement the quantity, as well as a submit order button which calls a method that prints out an order summary.
Unfortunately, I lost the original source code for the app, so I tried rebuilding it myself. I got it to work, but not in the way I originally designed it. The issue I'm having is that if I reference the TextView which stores the quantity of coffees ordered outside of its own method, the app immediately crashes due to a NullPointerException. However, if I reference that TextView by creating a method called display(int number), the app runs fine. What is causing this NullPointerException? I've provided links below to the two source codes, one of which works fine and the other of which crashes immediately.
NullPointerException: https://gist.github.com/CalebF98/eeb7d4144b831b247264ad6a3de0cb76 The part that is crashing the app in this code is at line 18 in the MainActivity.java file
Works fine: https://gist.github.com/CalebF98/906967d4ebe8ae4d1293a0a13b01f477 In this one, on lines 37-39, I create a method that holds the same code to reference the TextView as in the example above, which is later called by the methods for the + and - buttons.
Thank you!