我正在获取java.lang.NumberFormatException:无效的int:“ null”

时间:2019-11-03 19:06:44

标签: java android int

我已经看到许多与此有关的问题,但我无法解决此问题。我有一个带有用户要填充的EditTexts的表单,也有一个计算器。完成数学运算后,总保费就会变成一个值。然后,用户需要单击“提交”按钮以将详细信息添加到他的购物车,然后他可以从另一种相同类型的表格重新开始或进行结帐。如果他填写另一张表格,然后再次单击“提交”按钮。这些数据将显示在RecyclerView中,每种表格的总保费应添加到其中,并显示在购物车的底部,以便他可以付款。 问题是一旦我在数学运算后单击提交按钮即可继续购物车。该应用程序崩溃并给我“由java.lang.NumberFormatException引起:无效的int:“ null” ..请帮助。我看不到错误在哪里。完成数学运算后,我将结果转换为字符串,然后单击“提交”按钮,将其添加到购物车中。然后在购物车类上。表格应将所有总保费相加。

    Caused by: java.lang.NumberFormatException: Invalid int: "null"
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:410)
    at java.lang.Integer.parseInt(Integer.java:367)
    at java.lang.Integer.parseInt(Integer.java:334)
    at com.urbanx.urbaninsure.Cart.loadListProposals(Cart.java:167)
    at com.urbanx.urbaninsure.Cart.onCreate(Cart.java:113)
    at android.app.Activity.performCreate(Activity.java:6178)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5910) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

这是我的提案表单类的一部分,我在其中填写表单并提交

    private void fillProposalForm() {

    if (etFirstNameOfProposer.getText().toString().isEmpty()){
        etFirstNameOfProposer.setError("First name cannot be empty");
    } else{
        firstNameProposer = etFirstNameOfProposer.getText().toString();
    }
        if (etSurnameProposer.getText().toString().isEmpty()){
        etSurnameProposer.setError("Surname cannot be empty");
    } else {
            surnameProposer = etSurnameProposer.getText().toString();
        }
        if (etLastNameProposer.getText().toString().isEmpty()){
        etLastNameProposer.setError("Last name cannot be empty");
    } else
        {
            lastNameProposer = etLastNameProposer.getText().toString();
        }
        if (etPassportNo.getText().toString().isEmpty()){
        etPassportNo.setError("ID/Passport number cannot be empty");
    } else{
            idNumber = etPassportNo.getText().toString();
        } if (etPinNo.getText().toString().isEmpty()){
        etPinNo.setError("KRA pin number cannot be empty");

    }else {
            pinNo = etPinNo.getText().toString();
    }if (etMobile.getText().toString().isEmpty()){
        etMobile.setError("Mobile number cannot be empty");
    }else {
            mobile = etMobile.getText().toString();
    } if (etEmail.getText().toString().isEmpty()){
        etEmail.setError("Email cannot be empty");


    }else if (!Patterns.EMAIL_ADDRESS.matcher(etEmail.getText().toString()).matches()){
        etEmail.setError("Invalid email address");
    }
        else {
            email = etEmail.getText().toString();
    }if (etDrivingTime.getText().toString().isEmpty()){
        etDrivingTime.setError("Driving time duration cannot be empty");

    } else {
            drivingTime = etDrivingTime.getText().toString();
    }if (etDuration.getText().toString().isEmpty()) {
        etDuration.setError("Duration of insurance cannot be empty");

    } else {
            duration = etDuration.getText().toString();
    }if (etVehicelReg.getText().toString().isEmpty()){
        etVehicelReg.setError("Vehicle registration cannot be empty");

    } else {
            vehicleReg = etVehicelReg.getText().toString();
    }if (etEstimate.getText().toString().isEmpty()) {
        etEstimate.setError("The present estimated value of vehicle cannot be empty");
        bSubmit.setEnabled(false);


    } else {
            marketValue = etEstimate.getText().toString();
    }


    spTypeOfCoverage.setAdapter(TypeOfCoverageAdapter);
    spTypeOfCoverage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             typeOfCoverage = spTypeOfCoverage.getItemAtPosition(position).toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    spMake.setAdapter(MakeAdapter);
    spMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (i == 0) {

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 1) {
                spModel.setAdapter(subaruCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();


            } else if (i == 2) {
                spModel.setAdapter(toyotaCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();


            } else if (i == 3) {
                spModel.setAdapter(nissanCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();


            } else if (i == 4) {
                spModel.setAdapter(mercedesBenzCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();


            } else if (i == 5) {
                spModel.setAdapter(BMWCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();


            } else if (i == 6) {
                spModel.setAdapter(audiCarsAdapter);
                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 7) {
                spModel.setAdapter(landRoverCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 8) {
                spModel.setAdapter(hondaCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 9) {
                spModel.setAdapter(mazdaCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 10) {
                spModel.setAdapter(mitsubishiCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 11) {
                spModel.setAdapter(peugeotCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 12) {
                spModel.setAdapter(isuzuCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else if (i == 13) {
                spModel.setAdapter(volkswagenCarsAdapter);

                vehicleMake = spMake.getItemAtPosition(position).toString();

            } else {

                vehicleMake = spMake.getItemAtPosition(position).toString();

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    spUseOfVehicle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            useOfVehicle= spUseOfVehicle.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });



    spModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (spModel.getSelectedItem().toString().contains("Other")) {
                 vehicleModel = spModel.getSelectedItem().toString();


            } else {
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    spinnerModelYear.setAdapter(yearAdapter);
    spinnerModelYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            yearOfManf = spinnerModelYear.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
     if (etLimitWindscreen.getText().toString().isEmpty()){
         etLimitWindscreen.setText(String.valueOf(0));
     }

    windscreenValue = etLimitWindscreen.getText().toString();


    if (etLimitDvd.getText().toString().isEmpty()){

        etLimitDvd.setText(String.valueOf(0));

    }

    dvd = etLimitDvd.getText().toString();

    if ( etLimitRiot.getText().toString().isEmpty()){
        etLimitRiot.setText(String.valueOf(0));

    }


    riot = etLimitRiot.getText().toString();


    if ( etLimitExcessProtect.getText().toString().isEmpty()){
        etLimitExcessProtect.setText(String.valueOf(0));
    }
    excessProtector = etLimitExcessProtect.getText().toString();




    if (etLimitTerrorism.getText().toString().isEmpty()){
        etLimitTerrorism.setText(String.valueOf(0));
    }
    terrorism = etLimitTerrorism.getText().toString();


    if (etCourtesyCar.getText().toString().isEmpty()){
        etCourtesyCar.setText(String.valueOf(0));
    }
    courtesyCar = etCourtesyCar.getText().toString();


    calculateInsurance();

     proposalId = "number";


    currentProposer = new ProposalDetails(proposalId, firstNameProposer
            ,lastNameProposer,surnameProposer, idNumber, pinNo,
            email, mobile, drivingTime, duration, vehicleReg,
            marketValue, typeOfCoverage, useOfVehicle, vehicleMake, vehicleModel,
            yearOfManf, windscreenValue, dvd, riot,
            excessProtector, terrorism, courtesyCar, basicPremium, trainingLevy,
            policyHolders, stampDuty, totalPremium);
    currentProposer.setFirstNameProposer(firstNameProposer);
    currentProposer.setSurnameProposer(surnameProposer);
    currentProposer.setLastNameProposer(lastNameProposer);
    currentProposer.setEmail(email);
    currentProposer.setMobile(mobile);
    currentProposer.setDrivingTime(drivingTime);
    currentProposer.setIdNumber(idNumber);
    currentProposer.setVehicleReg(vehicleReg);
    currentProposer.setDuration(duration);
    currentProposer.setMarketValue(marketValue);
    currentProposer.setTypeOfCoverage(typeOfCoverage);
    currentProposer.setUseOfVehicle(useOfVehicle);
    currentProposer.setVehicleMake(vehicleMake);
    currentProposer.setVehicleModel(vehicleModel);
    currentProposer.setYearOfManf(yearOfManf);
    currentProposer.setWindscreenValue(windscreenValue);
    currentProposer.setDvd(dvd);
    currentProposer.setRiot(riot);
    currentProposer.setExcessProtector(excessProtector);
    currentProposer.setTerrorism(terrorism);
    currentProposer.setCourtesyCar(courtesyCar);
    currentProposer.setBasicPremium(basicPremium);
    currentProposer.setTrainingLevy(trainingLevy);
    currentProposer.setPolicyHolders(policyHolders);
    currentProposer.setStampDuty(stampDuty);
    currentProposer.setTotalPremium(totalPremium);


   // mProposalInformation.child(proposalId).setValue(currentProposer);

    new Database(getBaseContext()).addToCart(new Order(
            currentProposer.getFirstNameProposer(),
            currentProposer.getSurnameProposer(),
            currentProposer.getLastNameProposer(),
            currentProposer.getMobile(),
            currentProposer.getIdNumber(),
            currentProposer.getDuration(),
            currentProposer.getVehicleReg(),
            currentProposer.getTotalPremium()));







    startActivity(new Intent(ProposalForm1.this,Cart.class));

}



private void calculateInsurance() {
    double MarketValue,
            Duration ,
            TotalPremium;



    try {
        MarketValue = Double.valueOf(etEstimate.getText().toString());
    } catch (NumberFormatException e) {
        MarketValue = 0; // your default value
    }



    try {
        Duration = Double.valueOf(etDuration.getText().toString());
    } catch (NumberFormatException e) {
        Duration = 0; // your default value
    }


    double Windscreen ;


    try {
        Windscreen = Double.valueOf(etLimitWindscreen.getText().toString());
    } catch (NumberFormatException e) {
        Windscreen = 0; // your default value
    }

    double Riot  ;

    try {
        Riot = Double.valueOf(etLimitRiot.getText().toString());
    } catch (NumberFormatException e) {
        Riot = 0; // your default value
    }


    double Terrorism ;
    try {
        Terrorism = Double.valueOf(etLimitTerrorism.getText().toString());
    } catch (NumberFormatException e) {
        Terrorism = 0; // your default value
    }


    double Excessprotect ;
    try {
        Excessprotect = Double.valueOf(etLimitExcessProtect.getText().toString());
    } catch (NumberFormatException e) {
        Excessprotect = 0; // your default value
    }



    double Dvd ;
    try {
        Dvd = Double.valueOf(etLimitDvd.getText().toString());
    } catch (NumberFormatException e) {
        Dvd = 0; // your default value
    }


    double CourtesyCar ;

    try {
        CourtesyCar = Double.valueOf(etCourtesyCar.getText().toString());
    } catch (NumberFormatException e) {
        CourtesyCar = 0; // your default value
    }

    double Prata = 1;

    if (Duration ==  0){
        Prata = 1;

    }
    else if (Duration == 1){

        Prata = 0.3;
    }
    else if (Duration == 2){
        Prata = 0.4;
    }
    else if (Duration == 3){
        Prata = 0.5;
    }
    else if (Duration ==  4){
        Prata = 0.6;
    }
    else if (Duration ==  5){
        Prata = 0.7;
    }
    else if (Duration > 5){
        Prata = 1;
    }

    double Basicpremium = (MarketValue/100.0f) * coverageInt * Prata;
    etBasicPremium.setText(String.valueOf(Basicpremium));
    etBasicPremium.setText(""+Basicpremium);

    double Traininglevy = (Basicpremium/100.0f)* 0.45;
    etTrainingLevy.setText(String.valueOf(Traininglevy));
    etTrainingLevy.setText(""+Traininglevy);

    double PolicyholdersFund = (Basicpremium/100.0f) * 0.25;
    etPolicyHolders.setText(String.valueOf(PolicyholdersFund));
    etPolicyHolders.setText(""+PolicyholdersFund);

    double Stampduty = 40;

    etStampDuty.setText(String.valueOf(Stampduty));
    etStampDuty.setText(""+Stampduty);

    TotalPremium = Basicpremium + Traininglevy + PolicyholdersFund + Stampduty + Windscreen + Dvd + Riot + Terrorism + Excessprotect + CourtesyCar;

    int tp = (int) TotalPremium;
    totalPremium = String.valueOf(tp);
    etTotalPremium.setText(totalPremium);

    basicPremium = etBasicPremium.getText().toString();
    trainingLevy = etTrainingLevy.getText().toString();
    policyHolders = etPolicyHolders.getText().toString();
    stampDuty = etStampDuty.getText().toString();




}


    @Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.bSubimt:
            fillProposalForm();
            break;
    }
}

这是Cart类的一部分,给我一个错误

     private void loadListProposals() {
    cart = new Database(this).getCarts();
    adapter = new CartAdapter(cart,this);
    recyclerView.setAdapter(adapter);


    //Calculate totalPremium

    int total = 0;


    for (Order order:cart) {

     //   try
     //   {
      //      if( null != order.getTotalPremium() )
       //     {
                total += Integer.parseInt( order.getTotalPremium());

                Locale locale = new Locale("en", "US");
                NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

                txtTotalPrice.setText(fmt.format(total));

        //    }
      //  } catch (NumberFormatException ignored){

        }



    }

1 个答案:

答案 0 :(得分:1)

此处order.getTotalPremium()将“ null”保留为String。像String val = "null"