0

I'm trying to create a simple currency converter from USD, GBP, and Euro by entering an amount in a field, selecting the from currency in a spinner, and to currency in another spinner. I then output the converted amount in a text field.

But for some reason I cannot get past the onClick method and I do not know why. It might be something simple but I cannot see it.

Here is my code:

 public class Calculate extends Activity implements OnItemSelectedListener,
    OnClickListener {

Button Calculate;
String stringAmount;
double exchangeRate, E, D, Answer;
EditText Amount;
Spinner spinner, spinner1;
int ConvertFrom, ConvertTo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculate);
    Initilise();
    // this is calling the method initilise.
    // listens for the button to be clicked
    Calculate.setOnClickListener(this);

}

private void Initilise() {
    // Method initilise.
    /* Button */Calculate = (Button) findViewById(R.id.cal);
    /* EditText */Amount = (EditText) findViewById(R.id.amount);
    spinner = (Spinner) findViewById(R.id.Currency);
    spinner1 = (Spinner) findViewById(R.id.Currency1);

}

public void format() {

    ConvertFrom = 0;
    ConvertTo = 0;

    stringAmount = Amount.getText().toString();
    E = 0; // set it to 0 as the default
    try {
        E = Integer.parseInt(stringAmount);
    } catch (NumberFormatException e) {

    }
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    spinner();
    switch (v.getId()) {
    case R.id.cal:

        if (ConvertFrom == 0) {

            FromEuro();
        } else if (ConvertFrom == 1) {

            FromUsd();
        } else if (ConvertFrom == 2) {

            FromGbp();
        } else {
            // Amount.setText("Invalid Input");
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .create();
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Input a amount");
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();

        }
    }
}

public void spinner() {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, R.array.Currencies);

    spinner = (Spinner) findViewById(R.id.Currency);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    spinner1 = (Spinner) findViewById(R.id.Currency1);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    int posistion = spinner.getSelectedItemPosition();
    ConvertFrom = posistion;
    int posistion1 = spinner1.getSelectedItemPosition();
    ConvertTo = posistion1;
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

@SuppressWarnings("deprecation")
private void FromEuro() {
    // TODO Auto-generated method stub
    if (ConvertTo == 1) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 2) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 0) {
        AlertDialog alertDialog = new AlertDialog.Builder(null).create();
        alertDialog.setTitle("Input");
        alertDialog.setMessage("Please Select a gender");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // here you can add functions

            }
        });
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
    }
}

@SuppressWarnings("deprecation")
private void FromUsd() {
    // TODO Auto-generated method stub
    if (ConvertTo == 0) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 1) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else {
        if (ConvertTo == 2) {
            AlertDialog alertDialog = new AlertDialog.Builder(null)
                    .create();
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Select a gender");
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();
        }
    }
}

private void FromGbp() {
    // TODO Auto-generated method stub
    if (ConvertTo == 2) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else if (ConvertTo == 0) {
        exchangeRate = 1.336891;
        Answer = E * exchangeRate;
        String Final = Double.toString(Answer);
        Amount.setText(Final);

    } else {
        if (ConvertTo == 1) {
            Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle("Input");
            alertDialog.setMessage("Please Select a gender");
            alertDialog.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // here you can add functions

                        }
                    });
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.show();
        }
    }
}`

}

but i am now receiving

02-19 16:55:01.514: E/AndroidRuntime(28638): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)

the above error.


Previously this question had the following error:

Wondering if anyone can help. Im getting these errors:

FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.travelcurrencyconverter.Calculate.onClick(Calculate.java:64)
    at android.view.View.performClick(View.java:4222)
    at android.view.View$PerformClick.run(View.java:17273)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4895)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)
5
  • 1
    Could you also mark com.travelcurrencyconverter.Calculate.onClick(Calculate.java:64) line in your question? Commented Feb 19, 2013 at 13:07
  • You do not appear to be setting values for ConvertTo or ConvertFrom anywhere in your code. And if this holds true you will get an NPE when you attempt to use the ConvertTo value in your onClick method. Commented Feb 19, 2013 at 13:19
  • i knew i forgot something that has now moved me forward by showing me my alert dialog, now im guessing its not getting the data from the array via the spinners now but thanks you have moved me on there :D Commented Feb 19, 2013 at 13:27
  • Instead of if (...) { ... } else { if (...) { ... } else { ... } } you should use else if: if (...) { ... } else if (...) { ... } else { ... }. That way you won't have if's nested in if's nested in if's... making your code look like an arrow. Commented Feb 19, 2013 at 15:22
  • Thanks i've amended my code as your above suggestion. but now having problem one line 142 i will update my code above to latest version. Commented Feb 19, 2013 at 16:56

1 Answer 1

1

You're trying to access a member of a reference (the part before the dot) that is null on line 64.

Also, when you post code, indent all of it by four spaces to get all of it syntax colored.

Also, before you do that, make sure to auto format the code in your IDE.

Sign up to request clarification or add additional context in comments.

1 Comment

To whoever downvoted me: Why? Note that the question has been edited and when i submitted this answer it had the error message FATAL EXCEPTION: main java.lang.NullPointerException at com.travelcurrencyconverter.Calculate.onClick(Calculate.java:64)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.