-3

I am making a simple currency calculator for learning purposes and I just have no idea why my code won't work. Currently, when I press the "Convert" button there is a message saying "Appname stopped working"

Here is the code :

public class MainActivity extends AppCompatActivity {

    //Button convertButton;



    public void  convert(View view) {

        EditText dkk = (EditText) findViewById(R.id.dkkField);
        EditText gbp = (EditText) findViewById(R.id.gbpField);
        EditText eur = (EditText) findViewById(R.id.eurField);
        EditText usd = (EditText) findViewById(R.id.usdField);


        Double dkkNum = Double.parseDouble(dkk.getText().toString());
        Double gbpNum = Double.parseDouble(gbp.getText().toString());
        Double eurNum = Double.parseDouble(eur.getText().toString());
        Double usdNum = Double.parseDouble(usd.getText().toString());

        TextView textView = (TextView) findViewById(R.id.resultView);


        double dkkTogbp;
        double dkkToeur;
        double dkkTousd;
        double gbpToDkk;
        double eurToDkk;
        double usdToDkk;


        if(dkk!=null){

            dkkTogbp = dkkNum * 0.114001;
            dkkToeur = dkkNum * 0.134489;
            dkkTousd = dkkNum * 0.142729;

            textView.setText(dkk+ "DKK is "+ dkkTogbp+" GBP, "+dkkToeur+"EUR, "+ dkkTousd +"USD");


        }else if(gbp!=null){

            gbpToDkk = gbpNum * 8.77189;

            textView.setText(dkk+ "GBP is "+ gbpToDkk+" DKK");


        }else if(eur!=null){

            eurToDkk = eurNum * 7.43555;

            textView.setText(dkk+ "EUR is "+ eurToDkk+" DKK");

        }else if(usd!=null){

            usdToDkk = usdNum * 7.00630;

            textView.setText(dkk+ "USD is "+ usdToDkk+" DKK");
            Toast.makeText(getApplicationContext(), dkk.toString() + " DKK", Toast.LENGTH_LONG).show();

        }


        dkk.setText("");
        gbp.setText("");
        eur.setText("");
        usd.setText("");

    }

I discovered that when i remove the code block below and the if statements the program can run without crashing :

 Double dkkNum = Double.parseDouble(dkk.getText().toString());
        Double gbpNum = Double.parseDouble(gbp.getText().toString());
        Double eurNum = Double.parseDouble(eur.getText().toString());
        Double usdNum = Double.parseDouble(usd.getText().toString());

Can someone,please, explain why is this happening?

I don't get any errors.

Robert Ross
  • 1,151
  • 2
  • 19
  • 47

1 Answers1

1

The problem is by parsing the text to a double value.

At first, you should use a try-catch block to save your application for crashing, like this:

        try
        {
            Double dkkNum = Double.parseDouble(dkk.getText().toString());
            Double gbpNum = Double.parseDouble(gbp.getText().toString());
            Double eurNum = Double.parseDouble(eur.getText().toString());
            Double usdNum = Double.parseDouble(usd.getText().toString());
        }
        catch(NumberFormatException ex)
        {
            // If the format of the input string is wrong, then you get here the error message
        }
        catch(NullPointerException ex)
        {
            // If the input string is null, then you get here the error message
        }

Check, whether the text, that should be parsed, has the right syntax, like "1.9".

UPDATE: If the input string is null, the "parseDouble" method throws a "NullPointerException". For more informations see this link. I have updated the code.

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35