Reputation: 27
Hi I am making an app to calculate averages with user input from EditTexts. When I run the app it works, but when I leave a field blank it throws a 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. 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));
Upvotes: 0
Views: 266
Reputation: 146
Welcome to 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));
}
Upvotes: 0
Reputation: 3113
first of all don't initiate your views (eg : averageView) inside button click listener , put this line :
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[] :
EditText grades = ((EditText) findViewById(R.id.grade1));
if (!grades.getText().toString().isEmpty())
do your logic here
else
show some error
Upvotes: 0
Reputation: 73721
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
}
}
Upvotes: 1
Reputation: 7651
I would recommend to this to check if editText is empty or not :
if(editText.length()>0){
//there is text
}else{
//your code in case of empty editText
}
Upvotes: 0