[英]Prevent nullPointerException when EditText is empty
Hi I am making an app to calculate averages with user input from EditTexts. 嗨,我正在制作一个应用程序,使用EditTexts的用户输入来计算平均值。 When I run the app it works, but when I leave a field blank it throws a nullPointerException. 当我运行该应用程序时,它可以工作,但是当我将该字段保留为空白时,它将引发nullPointerException。 I know what it is and how I can fix them in general. 我知道它是什么,以及我一般如何修复它们。 With my code I was thinking of using an if statement to check if its empty. 对于我的代码,我正在考虑使用if语句检查其是否为空。 But I don't know how to check for a empty/null value with integers. 但是我不知道如何用整数检查一个空/空值。 Could someone explain whats the best way/practice for checking this? 有人可以解释什么是最好的方式/做法来检查此吗?
Any tips etc. are welcome! 欢迎任何提示等!
My code: 我的代码:
final Button calculateButton = findViewById(R.id.calculateAverageButton);
calaculateButton.setOnClickListener(new View.onClickListener() {
public void onClick(View v) { TextView averageView = findViewById(R.id.averageView);
int grade[] = {Integer.parseInt(((EditText) findViewById(R.id.grade1)).getText().toString());
int weight[] = {Integer.parseInt(((EditText) findViewById(R.id.weight1)).getText().toString());
int weightTotal = weight[0];
int sum = grade[0] * weight[0];
int average = sum / weightTotal
String averageText = getString(R.string.average);
averageView.setText(averageText + Integer.toString(average));
This is why you should avoid doing a lot of inline code. 这就是为什么您应该避免执行大量内联代码的原因。
There is no such thing as an empty integer, you need to check the string from the edit text 没有空整数这样的东西,您需要检查编辑文本中的字符串
String value = ((EditText) findViewById(R.id.grade1)).getText().toString();
if(value != null && value.length() > 0){
try{
//Since edit text gives you a string you cannot guarantee you have a number input
int numberValue = Integer.parseInt(value);
catch(NumberFormatException e){
//handle exception
}
}
I would recommend to this to check if editText is empty or not : 我建议对此检查editText是否为空:
if(editText.length()>0){
//there is text
}else{
//your code in case of empty editText
}
first of all don't initiate your views (eg : averageView) inside button click listener , put this line : 首先,不要在按钮单击监听器中启动您的视图(例如:averageView),将以下行放进去:
TextView averageView = findViewById(R.id.averageView);
to out of your button's click listener block 移出按钮的点击侦听器
String has a isEmpty() function so in order to check for empty string, before initiate your grade[] : 字符串具有isEmpty()函数,因此为了在开始运行grade []之前检查空字符串:
EditText grades = ((EditText) findViewById(R.id.grade1));
if (!grades.getText().toString().isEmpty())
do your logic here
else
show some error
Welcome to SO @J.Doe 欢迎来到SO @ J.Doe
There are many points about your code. 关于您的代码有很多要点。
anyway, i have tried to quickly mentionned some of them and modified it to look like this : 无论如何,我试图快速提及其中的一些并将其修改为如下所示:
final Button calculateButton = findViewById(R.id.calculateAverageButton);
EditText e1 = (EditText) findViewById(R.id.grade1);
EditText e2 = ((EditText) findViewById(R.id.weight1);
TextView averageView = findViewById(R.id.averageView);
calaculateButton.setOnClickListener(new View.onClickListener() {
public void onClick(View v) {
/** better practice for checking NullPointerException **/
if(e1.getText() || e2.getText()){
Log.d(TAG, "Null values");
return;
}
String txt1 = e1.getText().toString();
String tx2 = e2.getText().toString();
boolean emptyE1 = txt1.equals("");
boolean emptyE2 = txt2.equals("");
/** Checking for empty values **/
if(emptyE1 || emptyE2){
Log.d(TAG, "Empty values");
return;
}
int grade;
int weightTotal;
/** You have also to ensure that the strings are numeric**/
try{
grade = new Integer(txt1);
weightTotal = new Integer(txt1);
}
catch(NumberFormatException e){){
Log.d(TAG, "Number input required");
return;
}
/** Now, you are sure you have received numbers :) **/
int sum = grade * weight; // sum ? ;)
int average = sum / weightTotal
String averageText = getString(R.string.average);
averageView.setText(averageText + Integer.toString(average));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.