user2236096
user2236096

Reputation: 1607

Android app java.lang.RuntimeException error

Hi I'm trying to build a calculator to calculate compound interest, I'm keep getting a java.lang.RuntimeException error. I've read some topics on stackoverflow and their problems seems something wrong with their AndroidManifest.xml. I didn't edit that file at all but I'm still getting this error. Here's the code:

 package com.hychentsa.compoundinterest_v001;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import java.lang.Math;


    public class MainActivity extends Activity {
        private Button btn1;
        private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

        btn1 = (Button)findViewById(R.id.btn1);

        edt1 = (EditText)findViewById(R.id.edt1);
        edt2 = (EditText)findViewById(R.id.edt2);
        edt3 = (EditText)findViewById(R.id.edt3);
        edt4 = (EditText)findViewById(R.id.edt4);
        edt5 = (EditText)findViewById(R.id.edt5);
        edt6 = (EditText)findViewById(R.id.edt6);

        btn1.setOnClickListener(btn1listener);
    }
    double capital = Double.parseDouble(edt1.getText().toString());
    double everyyear = Double.parseDouble(edt2.getText().toString());
    double interest = Double.parseDouble(edt3.getText().toString());
    double year = Double.parseDouble(edt4.getText().toString());

    private Button.OnClickListener btn1listener = new Button.OnClickListener()
    {
        public void onClick (View v)
        {
            double capital = Double.parseDouble(edt1.getText().toString());
            double everyyear = Double.parseDouble(edt2.getText().toString());
            double interest = Double.parseDouble(edt3.getText().toString());
            double year = Double.parseDouble(edt4.getText().toString());
            double interestperyear = capital * Math.pow(1 + interest, year) + 
                    everyyear * (1 - Math.pow(1 + interest, year)) / interest * -1;
            edt5.setText(String.valueOf(interestperyear));
            edt6.setText(String.valueOf(interestperyear / 12));
        }
    };
    @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;
    }

}

And here's the logcat:

07-15 13:45:23.640: D/AndroidRuntime(1763): Shutting down VM
07-15 13:45:23.650: W/dalvikvm(1763): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 13:45:23.660: E/AndroidRuntime(1763): FATAL EXCEPTION: main
07-15 13:45:23.660: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.compoundinterest_v001/com.hychentsa.compoundinterest_v001.MainActivity}: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.os.Looper.loop(Looper.java:137)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at dalvik.system.NativeStart.main(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): Caused by: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763):     at com.hychentsa.compoundinterest_v001.MainActivity.<init>(MainActivity.java:30)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.Class.newInstanceImpl(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at java.lang.Class.newInstance(Class.java:1319)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-15 13:45:23.660: E/AndroidRuntime(1763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-15 13:45:23.660: E/AndroidRuntime(1763):     ... 11 more

Upvotes: 1

Views: 6888

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Remove the below which is before button listener. This causes NPE. Its outside of the method. Also you have the same inside button click so you can remove the ones outside the methods.

double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());

Upvotes: 1

njzk2
njzk2

Reputation: 39386

double capital = Double.parseDouble(edt1.getText().toString());

Happens at the init of your activity, which is much before you call edt1 = (EditText)findViewById(R.id.edt1);.

You can declare your double capital ... in the implicit init of the class, but the reference to edt1 must be after onCreate is called.

Also, the actual content of edt1.getText is not relevant even at this point as it is empty, hence will also cause an Exception.

Note that declaring the variable under the onCreate method doesn't make it be called after onCreate.

I don't think you need those 4 lines al all:

double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());

Upvotes: 1

Related Questions