I'm having the following problem: In my project have two field (editText) where the user can or not enter the value.
EditText that receives the amount etCount
.
EditText that receives the value etPriceUnit
.
When etCount
and etPriceUnit
are set to "null" at the time of calculation, the application closes.
This probably occurs when attempting to convert to double and multiply null values in line double doubleTOTAL_PRICE_ITEM = (Double.parseDouble(stringCOUNT) * Double.parseDouble(stringPRICE_UNIT));
.
I tried the following to fix such a 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 can not use the variable created inside if
on 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. Here's 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);
}