Error multiplying null values java

0

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);
}
    
asked by anonymous 01.04.2017 / 20:28

1 answer

3

Well to solve your problem I'll give you a way to set the value 0 to both the doubleCOUNT variable and the doublePRICE_UNIT variable when editText has nothing written.

Add the addTextChangedListener() method to your two editTexts ( etCount and etPriceUnit ) and do the following:

etCount :

etCount.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (s.length()>0){ //essa condição só executará se o editText não estiver vazio e fará com que seja adicionado o valor digitado no editText à variável doubleCOUNT

            doubleCOUNT=Double.parseDouble(etCount.getText().toString().trim());

        }else{ //essa condição fará com que seja atribuído o valor 0 à variável doubleCOUNT se não tiver nada escrito no editText (etCount)

            doubleCount=0;
            etCount.setHint("0");

        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

Just do the same thing with etPriceUnit.

Remembering that you can not (at least not for what you're trying to do) multiply strings because STRINGS variables are not only prepared to receive numbers, but also letters and special digits.

    
01.04.2017 / 21:47