-1

I have an EditText in which I get numbers and need to convert them to a float but still get the same eror - Null Pointer Exception.

This is my code:

public class Basic extends Fragment implements AdapterView.OnItemSelectedListener {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.basic, container, false);
        Spinner spinnerLenght = (Spinner) view.findViewById(R.id.spinnerLenght);
        spinnerLenght.setOnItemSelectedListener(this);
        ArrayAdapter<String> lenghtAdapter;
        lenghtAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.basic_string_lenght));
        lenghtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLenght.setAdapter(lenghtAdapter);

        return view;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String itemLenght = parent.getItemAtPosition(position).toString();
        EditText editTextLenght = (EditText) view.findViewById(R.id.editTextLenght);
        float floatLenght;
        floatLenght = Float.parseFloat(editTextLenght.getText().toString());
        float result_mm = 0;
        if (itemLenght.equals("mm")){


            result_mm = floatLenght * 1000;
        }


        Toast.makeText(parent.getContext(), (int) result_mm, Toast.LENGTH_LONG).show();
    }




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

    }
}

This is logcat :

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.dusan.unit, PID: 5181
                                                                      java.lang.NullPointerException
                                                                          at com.example.dusan.unit.Basic.onItemSelected(Basic.java:46)
                                                                          at android.widget.AdapterView.fireOnSelected(AdapterView.java:956)
                                                                          at android.widget.AdapterView.access$200(AdapterView.java:49)
                                                                          at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:920)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:157)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5356)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                                                                          at dalvik.system.NativeStart.main(Native Method)

I tried it on a double too. I also had problem with null = "" I read a lot about this but can`t find solution.

Eddie
  • 171
  • 5
  • 13

3 Answers3

0

1)Check if you wrote correct id of your editText; 2)Find your editText in onCreateView from inflated layout or make your layout view as class variable and then find your edit text from her in onItemClick

Vanya Makhlinets
  • 248
  • 1
  • 4
  • 13
  • I use tabbed activity here.And i don`t have onCreate here i have onCreateView and someone told me that is problem.Is that right? – Dusan Culum May 23 '17 at 21:40
  • @DusanCulum The tabbed activity does have `onCreate`. The fragment in a tab has `onCreateView`, but that is not the problem – OneCricketeer May 23 '17 at 21:42
0

Main problem

You are using the wrong ID and class.

EditText editTextLenght = (EditText) view.findViewById(R.id.editTextLenght);

The correct ID is android.R.id.text1.
The correct class is TextView.

See What is "android.R.layout.simple_list_item_1"? since that is the layout you set in the adapter.


Alternate solution

If the problem is parent.getItemAtPosition(position).toString();, then don't use that.

If you want to access the data in the adapter, then move it to a scope that you can access using a member variable

public class Basic extends Fragment implements AdapterView.OnItemSelectedListener {

    private ArrayAdapter<String> lenghtAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.basic, container, false);
        Spinner spinnerLenght = (Spinner) view.findViewById(R.id.spinnerLenght);
        spinnerLenght.setOnItemSelectedListener(this);
        this.lenghtAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.basic_string_lenght));

Then, use getItem method rather than trying to find the view

float floatLenght = Float.parseFloat(lenghtAdapter.getItem(position));

Also, please spell length correctly

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Still have problem logcat: java.lang.NullPointerException at com.example.dusan.unit.Basic.onItemSelected(Basic.java:39) ( String itemLenght = spinnerLenght.getSelectedItem().toString();) – Dusan Culum May 23 '17 at 21:50
  • `spinnerLenght` is not accessible within `onItemSelected` from your question, and that code you pasted doesn't match the question. So that is a **new** error, not the same – OneCricketeer May 23 '17 at 21:53
  • FATAL EXCEPTION: main Process: com.example.dusan.unit, PID: 24497 java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText at com.example.dusan.unit.Basic.onItemSelected(Basic.java:44) 44 is this :EditText editTextLenght = (EditText) view.findViewById(android.R.id.text1); – Dusan Culum May 23 '17 at 21:54
  • That makes sense... It's not an editable text field. Please try the *alternate solution*, then. – OneCricketeer May 23 '17 at 22:01
  • But in Spinner i chose mm cm or m so i need it first as string for if (itemLenght.equals("mm")){ result_mm = floatLenght * 1000; } – Dusan Culum May 23 '17 at 22:06
0

Look at main(you asked) problem, you want convert the number to float from the EditText.

First import: import android.widget.EditText;

EditText editTextLenght = (EditText) View.findViewById(R.id.editTextLenght);

floatLenght = Float.parseFloat(editTextLenght.getText().toString());
//this code used in java programs.in android you can use below

try{
   float floatLenght =  Float.valueOf(editTextLenght.getText().toString());
} catch (NumberFormatException e) {
   e.printStackTrace();
}

float(first letter is small) is primitive type and Float is wrapper class use to conversion from string to float. You don't need to change other code.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
  • java.lang.NullPointerException at com.example.dusan.unit.Basic.onItemSelected(Basic.java:53) that is float floatLenght = Float.valueOf(editTextLenght.getText().toString()); – Dusan Culum May 24 '17 at 11:00