0

My app crashes after it executes and shows 'Error: NullPointerException'. Don't know what to do. I am trying to make a simple currency converter in eclipse using Android SDK. If anyone can tell me how to make one it wolud be great!.(newbie to android and eclipse)

package com.example.converter;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  

public class MainActivity extends Activity implements OnClickListener{
EditText et = (EditText) findViewById(R.id.editText1);
EditText et1 = (EditText) findViewById(R.id.editText2);
TextView textView = (TextView) findViewById(R.id.textView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(this);


}

    public void onClick(View v) {
        // TODO Auto-generated method stub

        //EditText edit = (EditText) findViewById(R.id.editText1);
        String value = et.getText().toString(); // get the value from editText
        String result =convertToAED(value);
}

@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;
}

public String convertToAED(String value)
{   double input = 0;
    try{
     input = Double.parseDouble(value);
       }catch(NumberFormatException e)
       {
           e.printStackTrace();
       }
    double rate = Double.parseDouble(et1.getText().toString());
    double output = input*rate;
    return output+"";
}

public String convertToUSD(String value)
{try{
     input = Double.parseDouble(value);
       }catch(NumberFormatException e)
       {
           e.printStackTrace();
       }
    double rate = Double.parseDouble(et1.getText().toString());
    double output = input*rate;
    return output+"";
}

}

XML File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="27dp"
    android:ems="10"
    android:hint="@string/edit" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:prompt="@array/Currency"
    tools:listitem="@android:layout/simple_spinner_dropdown_item" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="121dp"
    android:hint="@string/output"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/spinner1"
    android:hint="Enter rate"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:onClick="onClick"
    android:layout_centerVertical="true"
    android:text="@string/Convert" />

</RelativeLayout>

Logcat:

11-24 01:11:18.720: D/dalvikvm(2030): Not late-enabling CheckJNI (already on)
11-24 01:11:23.960: I/Choreographer(2030): Skipped 484 frames!  The application may be doing too much work on its main thread.
11-24 01:11:24.250: D/gralloc_goldfish(2030): Emulator without GPU emulation detected.
11-24 01:11:33.440: D/AndroidRuntime(2030): Shutting down VM
11-24 01:11:33.440: W/dalvikvm(2030): threadid=1: thread exiting with uncaught exception (group=0xb0f4b648)
11-24 01:11:33.470: E/AndroidRuntime(2030): FATAL EXCEPTION: main
11-24 01:11:33.470: E/AndroidRuntime(2030): java.lang.NumberFormatException: Invalid double: "android.widget.EditText{b11f6860 VFED..CL .F....I. 24,250-456,309 #7f080002 app:id/editText1}"
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.initialParse(StringToReal.java:114)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.Double.parseDouble(Double.java:295)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.example.converterbase.MainActivity.conevrt(MainActivity.java:43)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.example.converterbase.MainActivity$1.onClick(MainActivity.java:27)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.view.View.performClick(View.java:4240)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.view.View$PerformClick.run(View.java:17721)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Handler.handleCallback(Handler.java:730)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.os.Looper.loop(Looper.java:137)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.reflect.Method.invokeNative(Native Method)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at java.lang.reflect.Method.invoke(Method.java:525)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 01:11:33.470: E/AndroidRuntime(2030):     at dalvik.system.NativeStart.main(Native Method)
11-24 01:11:36.240: I/Process(2030): Sending signal. PID: 2030 SIG: 9

1 Answer 1

1

Change to

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        EditText edit = (EditText) findViewById(R.id.editText1);
        String value = edit.getText().toString(); // get the value from editText
        String result =conevrt(value); // pass the value as param 

    }

You are creating an instance of MainActivity MainActivity ma = new MainActivity() which is wrong. Activity has a lifecycle.

Also

http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)

  try
  {
   Double input = Double.parseDouble(value); // numberformatexception if value is not a double value
  }catch(NumberFormatException e)
  {
       e.printStacktrace();
  }
Sign up to request clarification or add additional context in comments.

6 Comments

it fixed some problem but the app still crashes. '11-24 06:27:01.050: E/AndroidRuntime(878): FATAL EXCEPTION: main' '11-24 06:27:01.050: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.converter/com.example.converter.MainActivity}: java.lang.NullPointerException'
@user3026643 which line causes NPE
It shows around here '11-24 13:06:50.010: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method) 11-24 13:06:50.010: E/AndroidRuntime(780): Caused by: java.lang.NullPointerException 11-24 13:06:50.010: E/AndroidRuntime(780): at android.app.Activity.findViewById(Activity.java:1883)
@user3026643 check the ids in xml should match when you initialize views. Also indentify the line that causes NPE
I have gone through ids and it looks fine but in case of NPE i have no idea why it shows that error.
|

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.