My app keeps crashing and gives me a:
java.lang.NumberFormatException: empty String
Also says this
FATAL EXCEPTION: main
Process: com.example.ayyan.jellybeanestimator, PID: 2966
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity}: java.lang.NumberFormatException: empty String
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.view.View;
public class MainActivity extends AppCompatActivity {
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace, volumeOfOneJellyBean, volumeofBeans;
final 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) {
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
}
}
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
All your EditTexts start as empty. You can't getText()
in onCreate
, you have to do it in onClick
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
Note: when volumeOfOneJellyBean == 0
, you'll get a division by zero error
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.