Note that there are some explanatory texts on larger screens.

plurals
  1. PObasic calculator android development failing
    primarykey
    data
    text
    <p>I was following a tutorial on creating a temperature converter and it worked. Then I thought let me develop a calculator (basic edit text, text view and button manipulation) to practice further using these widgets.</p> <p>I wrote the code below but it is failing for some reason. I believe it is a NullPointerException when passing to result.setText() but I even hardcoded a string there, yet it doesn't work.First code is my code and the other code is the converter that worked fine with me:</p> <pre><code>public class MainActivity extends Activity { private EditText firstNumber; private EditText secondNumber; private TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void calculate(View view) { switch(view.getId()) { case R.id.additionButton: firstNumber = (EditText) findViewById(R.id.editText1); secondNumber = (EditText) findViewById(R.id.editText2); if(firstNumber.getText().length() != 0 &amp;&amp; secondNumber.getText().length() != 0) { float x = Float.parseFloat(firstNumber.getText().toString()); float y = Float.parseFloat(secondNumber.getText().toString()); result.setText(String.valueOf(addition(x, y))); } break; } } private float addition(float x, float y) { return x + y; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre> <p>and the converter code:</p> <pre><code>public class MainActivity extends Activity { private EditText text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (EditText) findViewById(R.id.editText1); } // This method is called at button click because we assigned the name to the // "OnClick property" of the button public void Convert(View view) { switch (view.getId()) { case R.id.button1: RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); if (text.getText().length() == 0) { Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show(); return; } float inputValue = Float.parseFloat(text.getText().toString()); if (celsiusButton.isChecked()) { text.setText(String .valueOf(convertFahrenheitToCelsius(inputValue))); celsiusButton.setChecked(false); fahrenheitButton.setChecked(true); } else { text.setText(String .valueOf(convertCelsiusToFahrenheit(inputValue))); fahrenheitButton.setChecked(false); celsiusButton.setChecked(true); } break; } } // Converts to celsius private float convertFahrenheitToCelsius(float fahrenheit) { return ((fahrenheit - 32) * 5 / 9); } // Converts to fahrenheit private float convertCelsiusToFahrenheit(float celsius) { return ((celsius * 9) / 5) + 32; } } </code></pre> <p>Please help me out as I am starting android development :) Thanks a bunch!</p> <p>NOTE: I said I believe because I am still getting used to debugger. This is the error msg it is showing:</p> <blockquote> <p>07-06 12:31:39.236: E/AndroidRuntime(28638): Caused by: java.lang.NullPointerException 07-06 12:31:39.236: E/AndroidRuntime(28638): at com.khalid.basiccalulator.MainActivity.calculate(MainActivity.java:34)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload