-1

I'm new to app development. I can run my application from Android Studio on a real device. I followed a tutorial. Everything ran until I wrote the code that sums both numbers and I can't seem to fix it.

Here is the error message I get along with the code in Main.

Main+Exception
(Click image to enlarge)

I don't understand most of this, and I can't continue without running the app. I don't run it on a virtual machine because it takes forever to load.

karel
  • 5,489
  • 46
  • 45
  • 50
  • 3
    Please add your code and error messages as text snippets, rather than images – Kai Mar 12 '19 at 22:27
  • 2
    Also please [edit your question](https://stackoverflow.com/posts/55131441/edit) to add the contents of `layout/activity_main.xml` to you question *as text*. – Michael Dodd Mar 12 '19 at 22:37
  • Your button add has a null value, that's why you can't set a listener for it, so you better check your xml file, maybe you initialize the add button with a wrong id? – Alexander Gapanowich Mar 12 '19 at 22:38
  • I would also vote this to be a duplicate of [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), but I'm out of close votes – Michael Dodd Mar 12 '19 at 22:39
  • Yes I saw the topic and I understood the problem. I am sorry for not posting my question under standards and I also failed to Copy/Paste the code because it showed and error that I need ot indent it but I just couldnt do it. Nonetheless I completly agree on marking the topic as duplicate. Have a good day! – Nikolay Petrov Mar 13 '19 at 04:59

1 Answers1

0

You should care about input must be number by adding this to both EditText in xml

 android:inputType="number"

also add if statement to check EditText not have Null Value(not empty), you can use such code to check if EditText empty

    int num1,num2;

    if(firstNumEditText.getText().toString().isEmpty()) {
         // is empty
         return;
    } else {
         // is not empty
        num1 = Integer.parseInt(firstNumEditText.getText().toString());
    }

    if(secondEditText.getText().toString().isEmpty()) {
         // is empty
         return;
    } else {
         //  is not empty
       num2 = Integer.parseInt(secondEditText.getText().toString());
    }

    resultTextView.setText(String.valueOf(num1+num2);
Dreamer
  • 517
  • 1
  • 4
  • 11