0

I'm trying to loop through an array of EditText id's to get all of their texts and add them to a single message. I understand how NullPointers work, but I cannot understand how temp and temp2 are NullPointers if I'm initializing them just as I have before, just this time it is in a loop. This is the error I get:

Process: com.example.brandon.netflixcalculator, PID: 6369
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.brandon.netflixcalculator.Exact_Activity.getInfo(Exact_Activity.java:152)
at com.example.brandon.netflixcalculator.Exact_Activity.updateFile(Exact_Activity.java:125)
at com.example.brandon.netflixcalculator.Exact_Activity$Listener.onClick(Exact_Activity.java:76)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Here is the method:

int[] editTextIds = {R.id.editTextMon, R.id.editTextTues, R.id.editTextWed, R.id.editTextThurs, R.id.editTextFri,
R.id.editTextSat, R.id.editTextSun};
int[] spinnerIds = {R.id.spinnerMon, R.id.spinnerTues, R.id.spinnerWed, R.id.spinnerThurs, R.id.spinnerFri,
       R.id.spinnerSat, R.id.spinnerSun};


public String getInfo(){
    String msg = "";
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < editTextIds.length; i++){
        EditText temp = (EditText)findViewById(editTextIds[i]);
        Spinner temp2 = (Spinner)findViewById(spinnerIds[i]);
        sb.append(temp.getText().toString());
        sb.append(".");
        sb.append(temp2.getSelectedItem().toString());
        sb.append("\n");
    }
    msg = sb.toString();
    return msg;
}

Here is my layout. Each of the days of the week is its own fragment, so here is Monday's (they are all the same besides the id's of each component):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.brandon.netflixcalculator.Monday"
style="@style/Main">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|top">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextMon"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/apptheme_edit_text_holo_light"
        android:hint="Average number of available viewing minutes on Monday"
        android:numeric="integer"
        android:layout_marginTop="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Probability of above viewing time on Monday"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:textAlignment="center"
        android:layout_marginTop="15dp" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinnerMon"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp" />
</LinearLayout>

Brandon Loehle
  • 256
  • 1
  • 3
  • 16
  • I just read it and tried their fix but I'm getting the same error – Brandon Loehle Feb 24 '16 at 00:13
  • Can you share your layout file code – sanky jain Feb 24 '16 at 00:21
  • @EJP, it's not about NPE, it's about why findByViewId returns null. Please reopen, so I can answer – Gavriel Feb 24 '16 at 00:25
  • @Gavriel if this post remains closed, would it be possible to tell me the answer here in the comments? I could really use your help – Brandon Loehle Feb 24 '16 at 00:30
  • Your code seems to be in an onClickListener. The View you get in the callback is probably the button, and you mean to search for the ids in the parent view: view.getParent().findByViewId Or it is in a fragment in which case getActivity().findByViewId – Gavriel Feb 24 '16 at 00:33

0 Answers0