1
votes

App crashes and I am getting these errors:


FATAL EXCEPTION: main Process: com.example.ayyan.jellybeanestimator, PID: 2960 java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity}: java.lang.NumberFormatException: empty String at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NumberFormatException: empty String at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) at java.lang.Double.parseDouble(Double.java:547) at com.example.ayyan.jellybeanestimator.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

package com.example.ayyan.jellybeanestimator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;   
import android.content.Intent;
import android.view.View;

public class MainActivity extends AppCompatActivity {
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace;
double loadFactor = .698;
TextView answer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    jellyBeanLength = (EditText) findViewById (R.id.length);
    jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
    jarSizeVolume = (EditText) findViewById (R.id.jarsize);
    jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
    jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
    jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
    answer = (TextView) findViewById (R.id.answer);
    Button calculate = (Button) findViewById (R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, MainActivity.class));
        }
    });



    double volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
    double volumeofBeans = (jellybeanspace*loadFactor);
    float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
    answer.setText("You have this amount of beans in your jar" + numberOfBeans);
}

}
5
"App crashes without errors. Getting these errors:" ???paxdiablo
It has errors. They just aren't compile time errors. Not all are. Your problem is: Caused by: java.lang.NumberFormatException: empty StringGabe Sechan
you are converting empty string to double which is not allowed. So firstly check whether its empty or not , than after parse :)Anand Savjani
The crash is caused by NumberFormatException and the reason behind it is that one (or more) of your edit text's text is empty. Before converting your edit text's text to double, check if the texts are empty and also whether they are valid numbers or not.Srikar Reddy
startActivity(new Intent(MainActivity.this, MainActivity.class)); Why you call the same activityRaj

5 Answers

2
votes

Check if you have any value inserted in the EditText then only parse it in Double

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        jellyBeanLength = (EditText) findViewById (R.id.length);
        jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
        jarSizeVolume = (EditText) findViewById (R.id.jarsize);

        answer = (TextView) findViewById (R.id.answer);
        Button calculate = (Button) findViewById (R.id.calculate);
        calculate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

//Take out value when button pressed
        jellybeantall = Double.parseDouble(TextUtils.isEmpty(jellyBeanLength.getText().toString())?"0":jellyBeanLength.getText().toString());
        jellybeanfat = Double.parseDouble(TextUtils.isEmpty(jellyBeanDiameter.getText().toString())?"0":jellyBeanDiameter.getText().toString());
        jellybeanspace = Double.parseDouble(TextUtils.isEmpty(jarSizeVolume.getText().toString())?"0":jarSizeVolume.getText().toString());



        double volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
        double volumeofBeans = (jellybeanspace*loadFactor);
        float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
        answer.setText("You have this amount of beans in your jar" + numberOfBeans);
            } 
        }); 
    } 
1
votes

you should calculate onClick of button.In your code you are not entering any value and trying to calculate it.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    jellyBeanLength = (EditText) findViewById (R.id.length);
    jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
    jarSizeVolume = (EditText) findViewById (R.id.jarsize);
    jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
    jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
    jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
    answer = (TextView) findViewById (R.id.answer);
    Button calculate = (Button) findViewById (R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    double volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
    double volumeofBeans = (jellybeanspace*loadFactor);
    float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
    answer.setText("You have this amount of beans in your jar" + numberOfBeans);
        }
    });
}
0
votes

There are no values in the EditText field and you are trying to parse that value.Hence the error java.lang.NumberFormatException: empty String.

-2
votes
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try
    {
       jellyBeanLength = (EditText) findViewById (R.id.length);
        jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
        jarSizeVolume = (EditText) findViewById (R.id.jarsize);
        jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
        jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
        jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
        answer = (TextView) findViewById (R.id.answer);
        Button calculate = (Button) findViewById (R.id.calculate);
        calculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        double volumeOfOneJellyBean =  ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
        double volumeofBeans = (jellybeanspace*loadFactor);
        float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
        answer.setText("You have this amount of beans in your jar" + numberOfBeans);
        }
        });
    } 
    catch (Exception e) 
    {
       e.printStackTrace();
    }
}
-3
votes

you could try changing:

answer.setText("You have this amount of beans in your jar" + numberOfBeans);

to

answer.setText("You have this amount of beans in your jar" + String.valueOf(numberOfBeans));

This should convert the double to a string.