Pavithran
Pavithran

Reputation: 9

How can I validate EditText. App is getting crashed on submit without any value in EditText

I'm trying to validate EditText for mandatory check. So I created Validation.java with hasText() function. Even after using this my app is getting crashed when no value is passed in EditText instead of displaying warning. Attaching my code below.

Homescreen.class

package com.test.tax;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.text.Editable;

import java.text.DecimalFormat;

import static com.test.tax.R.id.editText;
import static java.lang.Math.round;

public class HomeScreen extends AppCompatActivity {

    public  static String  Buy_Price = "a";
    public  static String  Sell_Price = "b";
    public  static String  QUANTITY = "c";
    public  static String  BROKERAGE = "d";
    public  static String  ActualProfitLoss_String = "e";
    public  static String  TurnOver_String = "f";
    public  static String  STT_String = "g";
    public  static String  ServiceTax_String = "h";
    public  static String  TotalCharge_String = "i";
    public  static String  x = "j";
    private static  String Strin ;

    EditText editText;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    Button btnSubmit;

    boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
    }



    public void calculateProfit(View view) {
        Intent intent = new Intent(this, displayProfit.class);
        Bundle extras =new Bundle();
        editText=(EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText4=(EditText) findViewById(R.id.editText4);
        editText4.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText4);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText2=(EditText) findViewById(R.id.editText2);
        editText2.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText2);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText3=(EditText) findViewById(R.id.editText3);
        editText3.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText3);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });

        btnSubmit = (Button) findViewById(R.id.button);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                Validation class will check the error and display the error on respective fields
                but it won't resist the form submission, so we need to check again before submit
                 */
            }
        });


        Log.d(Strin, "editText ::::");


        String buyPriceString = editText3.getText().toString();
        Float BuyPrice =  Float.parseFloat(buyPriceString);
        String quantityString = editText2.getText().toString();
        Float Quantity =  Float.parseFloat(quantityString);
        String sellPriceString = editText4.getText().toString();
        Float SellPrice =  Float.parseFloat(sellPriceString);
        String brokerageString = editText.getText().toString();
        Float Brokerage =  Float.parseFloat(brokerageString);
        Log.d(Strin, "SellPrice ::::"+SellPrice);
        Log.d(Strin, "BuyPrice ::::"+BuyPrice);
        Log.d(Strin, "Quantity ::::"+Quantity);
        Log.d(Strin, "Brokerage ::::"+Brokerage);
        double TurnOver=(BuyPrice*Quantity)+(SellPrice*Quantity);
        double TurnOver1=roundTwoDecimals(TurnOver);
        Log.d(Strin, "TurnOver ::::"+TurnOver);
        double Sell1 =SellPrice-(SellPrice*(Brokerage/100));
        Log.d(Strin, "Sell1 ::::"+Sell1);
        double Buy1 = BuyPrice+(BuyPrice*(Brokerage/100));
        Log.d(Strin, "Buy1 ::::"+Buy1);
        double TotalBrokerage = ((Sell1*Brokerage/100)+(Buy1*Brokerage/100));
        Log.d(Strin, "TotalBrokerage ::::"+TotalBrokerage);


        double STT = (Quantity*(SellPrice-(SellPrice*(Brokerage/100))))*(.025/100);
        double STT1 = roundTwoDecimals(STT);

        Log.d(Strin, "STT ::::"+STT);
        double TrnxChrge = TurnOver*(0.00275/100);

        Log.d(Strin, "TrnxChrge ::::"+TrnxChrge);
        double ServiceTax = (TotalBrokerage+STT)*(.15);
        double ServiceTax1 = roundTwoDecimals(ServiceTax);

        Log.d(Strin, "ServiceTax ::::"+ServiceTax);
        double SEBICharge = (TurnOver*(.0002/100));
        Log.d(Strin, "SEBICharge ::::"+SEBICharge);
        double TotalCharge = ServiceTax+TrnxChrge+SEBICharge+STT;
        double TotalCharge1 = roundTwoDecimals(TotalCharge);
        Log.d(Strin, "TotalCharge ::::"+TotalCharge);
        double NetProfit = (Sell1-Buy1)*Quantity;
        Log.d(Strin, "NetProfit ::::"+NetProfit);

        double ActualProfitLoss = (NetProfit -TotalCharge);
        double ActualProfitLoss1 = roundTwoDecimals(ActualProfitLoss);
        String ActualProfitLossString=Double.toString(ActualProfitLoss1);
        String TurnOverString=Double.toString(TurnOver1);
        String STTString=Double.toString(STT1);
        String ServiceTaxString=Double.toString(ServiceTax1);
        String TotalChargeString=Double.toString(TotalCharge1);

        Log.d(Strin, "Pavi ::::"+ActualProfitLossString);

        extras.putString(ActualProfitLoss_String,ActualProfitLossString);
        extras.putString(TurnOver_String,TurnOverString);
        extras.putString(STT_String,STTString);
        extras.putString(ServiceTax_String,ServiceTaxString);
        extras.putString(TotalCharge_String,TotalChargeString);
        extras.putString(Buy_Price,buyPriceString);
        extras.putString(Sell_Price,sellPriceString);
        extras.putString(BROKERAGE,brokerageString);

        extras.putString(QUANTITY,quantityString);

        Log.d(Strin, "buyPrice ::::"+ActualProfitLoss);
//        intent.putExtra(Sell_Price,sellPriceString);
//        intent.putExtra(QUANTITY,quantity);
        intent.putExtras(extras);

        startActivity(intent);

    }

    private double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
}

Validation.java

public class Validation {
public static boolean hasText(EditText editText) {
    String text = editText.getText().toString().trim();
    editText.setError(null);
    // length 0 means there is no text
    if (text.length() == 0) {
        editText.setError("REQUIRED_Data");
        return false;
    }
    return true;
}
}

Debug Logs:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.test.tax, PID: 3290
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NumberFormatException: empty String
                  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                  at java.lang.Float.parseFloat(Float.java:459)
                  at com.test.tax.HomeScreen.calculateProfit(HomeScreen.java:121)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

Upvotes: 0

Views: 88

Answers (2)

Atef Hares
Atef Hares

Reputation: 4881

There error is in this line (and any similar line will throw the same error)

    Float BuyPrice =  Float.parseFloat(buyPriceString);

You have to always check if there is a value entered in you EditText before using it with the help of your Validation.hasText() .

For example, like So, these lines:

String buyPriceString = editText3.getText().toString();
Float BuyPrice =  Float.parseFloat(buyPriceString);

Should be like that

   if(Validation.hasText(editText3)){
            String buyPriceString = editText3.getText().toString();
            Float BuyPrice =  Float.parseFloat(buyPriceString);
        }

And remember to do that with all of your other edittexts

Also You may want to change the way of how Validation.hasText() works to this way:

 if (text.equals("") ) {
        editText.setError("REQUIRED_Data");
        return false;
    }

Upvotes: 0

zed
zed

Reputation: 3267

Your problem is showing in

          Caused by: java.lang.NumberFormatException: empty String
              at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
              at java.lang.Float.parseFloat(Float.java:459)
              at com.test.tax.HomeScreen.calculateProfit(HomeScreen.java:121)
              at java.lang.reflect.Method.invoke(Native Method) 

One of your parseFloat methods in calculateProfit is throwing this. It is due to have an empty String.

Before parsing to float, convert all empty strings to zero such as:

   buyPriceString = buyPriceString.length() > 0 ? buyPriceString : "0";

Upvotes: 0

Related Questions