1

Hi I'm taking a value from edittext and converting displaying that value in other text but getting run time error java.lang.NumberFormatException: Invalid double: "".at java.lang.StringToReal.invalidReal. In my setAdapter its taking only franheit that means only todetails. I didn't understand where the bug is

public class UniqueConverter extends Fragment {

    private Spinner fromDetails, toDetails;
    private EditText fromInput, toInput;
@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_uniquelayout, container, false);
onInitVies(view);
        int position = getArguments().getInt("position");
        String ifrom,rto;
        Double inp;
        double out = 0;

        switch (position) {
            case 0:
                setAdapter(temperatureform, "Celsius", "Fahrenheit");
                ifrom = (String) fromDetails.getSelectedItem();
                rto = (String) toDetails.getSelectedItem();
                inp = Double.valueOf(fromInput.getText().toString());
                TemperatureConverter con = new TemperatureConverter();
                TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ifrom);
                TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(rto);
                out = con.TemperatureConvert(fromUnit,toUnit,inp);
                break;
            case 1:
                setAdapter(weightform, "Kilograms", "Grams");
                ifrom = (String) fromDetails.getSelectedItem();
                rto = (String) toDetails.getSelectedItem();
                inp = Double.valueOf(fromInput.getText().toString());
                WeightConverter converter = new WeightConverter();
                WeightConverter.Inus fromInus = WeightConverter.Inus.fromString(ifrom);
                WeightConverter.Inus toInus = WeightConverter.Inus.fromString(rto);
                out = converter.WeightConvert(fromInus, toInus, inp);
                break;

  final double finalOut = out;
        fromInput.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) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    if (!fromInput.getText().toString().equals(""))
                    {
                        toInput.setText(String.valueOf(finalOut));
                    }
                    else
                    {
                        toInput.setText("");
                    }

                }
            });


            return view;

    }


    private void setAdapter(String[] spinnerItems, String fromDefaultText, String toDefaultText) {
        fromDetails.setAdapter(new CustomSpinnerAdapter(getContext(), R.layout.spinner_item, spinnerItems, fromDefaultText));
        toDetails.setAdapter(new CustomSpinnerAdapter(getContext(), R.layout.spinner_item, spinnerItems, toDefaultText));
    }

    private void onInitVies(View view) {
        fromDetails = (Spinner) view.findViewById(R.id.fromSpinner);
        toDetails = (Spinner) view.findViewById(R.id.toSpinner);

        fromInput = (EditText) view.findViewById(R.id.fromInput);
        toInput = (EditText) view.findViewById(R.id.toInput);
    }

logcat:

 Process: com.cloudicalabs.converters, PID: 8063
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime: java.lang.NumberFormatException: Invalid double: ""
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at java.lang.StringToReal.parseDouble(StringToReal.java:267)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at java.lang.Double.parseDouble(Double.java:301)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at java.lang.Double.valueOf(Double.java:338)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at com.cloudicalabs.converters.fragments.UniqueConverter.onCreateView(UniqueConverter.java:74)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
03-16 03:47:54.078 8063-8063/com.cloudicalabs.converters E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
7
  • can you please post your error line 74 @sun Commented Mar 16, 2016 at 9:07
  • If your string is empty or null than you will get NumberFormateExaception error.So before converting string to float or double you have check that your string is empty or not. Commented Mar 16, 2016 at 9:08
  • inp = Double.valueOf(fromInput.getText().toString()); this is my error line Commented Mar 16, 2016 at 9:09
  • You get an empty string in fromInput.getText().toString(); which can not converted to a double Commented Mar 16, 2016 at 9:11
  • Why are you using Double and double ? Commented Mar 16, 2016 at 9:15

5 Answers 5

3
try {
   inp = Double.valueOf(fromInput.getText().toString());
} catch (NumberFormatException e) {
   inp = 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try replacing this line

inp = Double.valueOf(fromInput.getText().toString());

with

inp = Double.parseDouble(fromInput.getText().toString());

5 Comments

a problem occur my setAdapter taking only todetails means it taking faranheit while debugging.
my spinners taking same value
where did you initialize those spinners ?
initialized in onInitVies method, please check in the code
Have you been released with NumberFormatException and another issue raised or still showing the same error ??
0
@Override
public void afterTextChanged(Editable s) {

    if (!TextUtils.isEmpty(fromInput.getText())
    {
      String value=null;
        try {
            value=String.valueOf(Double.parseDouble(fromInput.getText().toString()));
            toInput.setText(value);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            toInput.setText("");
        }
    }
    else
    {
        toInput.setText("");
    }
}

Comments

0

before doing this

inp = Double.valueOf(fromInput.getText().toString());

check whether the value from the fromInput text is having "" (empty -i.e holding nothing: say like .length == 0) then u shouldn't parse it; instead notify user to enter some value. also put it in try catch.

Comments

0

Your problem is that the variable or resource from which you want to get value is null and so it is saying that " " is not double. If you are getting value from fromInput.getText().toString() then fromInput has no value and thus null can't be cast to double.

1 Comment

he is doing it in initViews() function; he would have got NPE its basics :) @Parsania Hardik

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.