0
I’m having the following problem: In my project there are two fields (editText) where the user can or cannot enter the value.
Edittext that receives the amount etCount
.
Edittext that receives the value etPriceUnit
.
When the etCount
and the etPriceUnit
present the value "null" at the time of calculating the application closes.
This probably occurs in the attempt to convert to double and multiply null values on the line double doubleTOTAL_PRICE_ITEM = (Double.parseDouble(stringCOUNT) * Double.parseDouble(stringPRICE_UNIT));
.
I tried the following to fix such problem:
if (stringCOUNT == null || stringPRICE_UNIT == null) {
String stringTOTAL_PRICE_ITEM = "0";
} else {
Double doubleTOTAL_PRICE_ITEM = (Double.parse.Double(stringCOUNT) * Double.parseDouble(stringPRICE_UNIT));
String stringTOTAL_PRICE_ITEM = Double.toString(doubleTOTAL_PRICE_ITEM);
}
But I did not succeed. I cannot use the variable created within the if
on the line result = crud.insertProduct(stringPRODUCT_NAME, stringCOUNT, stringPRICE_UNIT, stringTOTAL_PRICE_ITEM);
.
Insertproduct.class (onClick):
public void onClick(View v) {
Controller crud = new Controller(getBaseContext());
EditText PRODUCT_NAME = (EditText) findViewById(R.id.etProductName);
EditText COUNT = (EditText) findViewById(R.id.etCount);
EditText PRICE_UNIT = (EditText) findViewById(R.id.etPriceUnit);
String stringPRODUCT_NAME = PRODUCT_NAME.getText().toString();
String stringCOUNT = COUNT.getText().toString();
String stringPRICE_UNIT = PRICE_UNIT.getText().toString();
double doubleTOTAL_PRICE_ITEM = (Double.parseDouble(stringCOUNT) * Double.parseDouble(stringPRICE_UNIT));
String stringTOTAL_PRICE_ITEM = Double.toString(doubleTOTAL_PRICE_ITEM);
String result;
if (stringPRODUCT_NAME.matches("")) {
Toast.makeText(InsertProduct.this, "Você não inseriu o nome do produto.", Toast.LENGTH_SHORT).show();
return;
}
result = crud.insertProduct(stringPRODUCT_NAME, stringCOUNT, stringPRICE_UNIT, stringTOTAL_PRICE_ITEM);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
Intent intent = new Intent(InsertProduct.this, Main.class);
startActivity(intent);
finish();
}
Controller.class (String insertProduct):
public String insertProduct(String PRODUCT_NAME, String stringCOUNT, String stringPRICE_UNIT, String stringTOTAL_PRICE_ITEM) {
ContentValues values;
long result;
db = database.getWritableDatabase();
values = new ContentValues();
values.put(CreateDB.PRODUCT_NAME, PRODUCT_NAME);
values.put(CreateDB.COUNT, stringCOUNT);
values.put(CreateDB.PRICE_UNIT, stringPRICE_UNIT);
values.put(CreateDB.TOTAL_PRICE_ITEM, stringTOTAL_PRICE_ITEM);
result = db.insert(CreateDB.TABLE, null, values);
db.close();
if (result == -1)
return "Erro ao inserir o produto.";
else
return "Produto inserido com sucesso!";
}
@Update Problem solved. Follows the solution.
String stringCOUNT = COUNT.getText().toString(); //Pegando o valor do etCount e convertendo para string
String stringPRICE_UNIT = PRICE_UNIT.getText().toString();
if (stringCOUNT.matches("")) { //Se o EditText estiver vazio faça
doubleCOUNT = 0; //Atribui o valor 0 se o EditText estiver vazio
} else { //Senão
doubleCOUNT = Double.parseDouble(stringCOUNT); //Pega o valor do EditText e converte para double
}
if (stringPRICE_UNIT.matches("")) {
doublePRICE_UNIT = 0;
} else {
doublePRICE_UNIT = Double.parseDouble(stringPRICE_UNIT);
}
have already tried to initialize the value of these variables as 0? so that if they are not modified they remain 0?
– Boneco Sinforoso
@Dolls symphooroso Olhá, I had not thought about it, I’m too lay. Could you tell me where I can declare the variable with value 0?
– Giovani Rodrigo
first it seems wrong to me that you try to perform mathematical operations with two strings, declare stringCOUNT and stringPRICE_UNIT as int or double.
– Boneco Sinforoso
To declare them with the value 0 do the following
double stringCOUNT = 0
anddouble stringPRICE_UNIT = 0
– Boneco Sinforoso
@I declare the variables in the
public class InsertProduct
, in functiononCreate()
or in functiononClick()
?– Giovani Rodrigo
etCount and etPriceUnit are two correct Edittexts?
– Boneco Sinforoso
Correct, I have already made the appropriate conversions declared in public class the following variables
double doubleCOUNT = 0;
anddouble doublePRICE_UNIT = 0;
, and converted todouble
in functiononClick()
doubleCOUNT = Double.parseDouble(COUNT.getText().toString());
anddoublePRICE_UNIT = Double.parseDouble(PRICE_UNIT.getText().toString());
– Giovani Rodrigo
I made a response to facilitate your understanding and abbreviate your code
– Boneco Sinforoso