0

I have a very crazy bug. When I create a TextView array and initialize them in onCreate with my id from xml, nothing is changed. I create a simple function to change anything, but I get a nullpointer exception. I don't have any idea what I'm doing wrong with my code.

I will be very gratefull for any help.

XML:

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
        android:layout_weight="10"
        >
    <LinearLayout
            android:id="@+id/keys"
            .....
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@+id/keys"
            android:weightSum="6" >
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="A"
                android:layout_weight="1"
                android:id="@+id/data1" android:layout_alignTop="@+id/linearLayout"
                android:layout_alignParentLeft="true"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="A"
                android:layout_weight="1"
                android:id="@+id/data2"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="A"
                android:layout_weight="1"
                android:id="@+id/data3"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="A"
                android:layout_weight="1"
                android:id="@+id/data4"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="A"
                android:layout_weight="1"
                android:id="@+id/data5"/>
    </LinearLayout>
</RelativeLayout>

CODE:

public class XXX extends Activity {

    private String login;
    ....
    private TextView[] dates;



    public void change(String x) {
       if(dates[0].getEditableText().toString().compareTo("") == 0)
          dates[0].setText(x);
    }



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ABC);
        dates = new TextView[5];
        dates[0]=(TextView)findViewById(R.id.data1);
        dates[1]= (TextView) findViewById(R.id.data2);
        dates[2]=(TextView)findViewById(R.id.data3);
        dates[3]=(TextView)findViewById(R.id.data4);
        dates[4]=(TextView)findViewById(R.id.data5);
        dates[0].setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        for (int i= 0; i < 5; i++){
            dates[i].setText("LOL");
        };
        change("ROTFL");
    }
pkruk
  • 809
  • 1
  • 7
  • 24
  • In your function `change` what is the value of dates[0] ? – Benjamin RD Mar 27 '14 at 20:05
  • 1
    Have you tried doing this with ArrayList where you initialize your ArrayList, ArrayList dates = new ArrayList();? I am pretty sure the way you have it setup right now will give a npe because dates isn't initialized. You could even try changing it to TextView[] dates = new TextView[10]; or something. Not 100%, just seems like a possibility – zgc7009 Mar 27 '14 at 20:11
  • @user3294396 I get a java.lang.NullPointerException – pkruk Mar 27 '14 at 20:21
  • @zgc7009 When I try with ArrayList I can change my Text in onCreate but in Change method, when I try a getEditableText().toString().compareTo("") == 0 I get NullPointerException, when I print with Log.d size() that's return me a correct size(). I don't know why I get a NullPointerException – pkruk Mar 27 '14 at 20:23
  • 1
    Change it to just getText() instead of getEditableText()... dates.get(i).getText().toString().compareTo("") == 0 – zgc7009 Mar 27 '14 at 20:25
  • Hmm okey at now everything is alright, when I change a getEditableText().toString() to get(0)).getText() everything is okey, but i still don't know what is wrong with getting editableText – pkruk Mar 27 '14 at 20:26
  • 1
    The problem is that you need initialize the `private TextView[] dates;` you need a constructor for it – Benjamin RD Mar 27 '14 at 20:48
  • Please check this answer this will works fine for me ... http://stackoverflow.com/a/12233299/3959610 – Kushal Apr 23 '15 at 13:13

0 Answers0