-2

I am making a simple app (seen in photo) all it does is count up and you have to try get to the goal. I'm trying to do the if statement part where if the current number is = to the goal make a new random number. When I try any thing in this if statement, even something that just makes the current number 0 it crashes the app.

enter image description here

if(currentNumber == goal){
        randomNumber = random.nextInt(100-0);
    }

.

.

Here's the one with simple code that makes the current number = 0

if(currentNumber == goal){
        currentNumber.setText("0");
    }

Stack trace:

enter image description here

Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
  • 3
    I would expect the crash to come with a stack trace - have you looked in the log? Have you stepped through the code in a debugger? – Jon Skeet May 30 '22 at 09:41
  • The error would help. I guess it has something to do with `goal` being a different datatype than `currentNumber` – XtremeBaumer May 30 '22 at 09:42
  • I should add, if the 'if statement' is left empty the app works as intended, there's just no way to complete the level. – CallMeJacob May 30 '22 at 09:42
  • 1
    if currentNumber is an actual declared variable, and is assigned a value, there is no way that if statement can actually 'crash' the application. – Stultuske May 30 '22 at 09:47
  • The if statement itself looks fine. Yet in the body of the upper one, you dereference the variable `random`. Could it be, that you don't set it to an instant? We could help you better if you could share the ExceptionStack thrown when crashing. – Valerij Dobler May 30 '22 at 09:47
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Stultuske May 30 '22 at 09:51
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – XtremeBaumer May 30 '22 at 09:51
  • 1
    for future reference, don't put a stacktrace as an image, but as formatted text – Stultuske May 30 '22 at 09:51
  • 2
    in `currentNumber.setText("0")`, `currentNumber` is null. That is the error, so check how you get hold of that variable – Bentaye May 30 '22 at 09:52
  • 1
    Welcome to Stack Overflow. Please read [ask] and [mre], and [do not upload images of errors when asking a question](//meta.stackoverflow.com/q/285551) - instead, copy and paste the stack trace, formatted like code. – Karl Knechtel May 30 '22 at 09:52

0 Answers0