yassine__
yassine__

Reputation: 393

android textView.setText() gives me null pointer exception

I've create a custom adapter to put images one my listView but is giving me a null pointer exception in my textView.setText(text).

this is the part when i get exception my adapter :

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(rowResourceId, parent, false);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageFileView);
        TextView textView = (TextView) rowView.findViewById(R.id.fileName);

        int pos = position+4;

        String name = Names[pos];

        String extFile = Names[pos].substring(Names[pos].lastIndexOf(".")+1);

        String uri = "drawable/"+extFile+".png";

        textView.setText(name);     

        Uri imgUri=Uri.parse(uri);
        imageView.setImageURI(imgUri);
        return rowView;  
    }

this is my LogCat when a i have exception:

06-14 12:15:04.616: E/AndroidRuntime(678): FATAL EXCEPTION: main
06-14 12:15:04.616: E/AndroidRuntime(678): java.lang.NullPointerException
06-14 12:15:04.616: E/AndroidRuntime(678):  at com.example.poc_cubbyhole.ItemAdapter.getView(ItemAdapter.java:59)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.ListView.fillSpecific(ListView.java:1290)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.ListView.layoutChildren(ListView.java:1588)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.os.Looper.loop(Looper.java:123)
06-14 12:15:04.616: E/AndroidRuntime(678):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 12:15:04.616: E/AndroidRuntime(678):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 12:15:04.616: E/AndroidRuntime(678):  at java.lang.reflect.Method.invoke(Method.java:507)
06-14 12:15:04.616: E/AndroidRuntime(678):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 12:15:04.616: E/AndroidRuntime(678):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 12:15:04.616: E/AndroidRuntime(678):  at dalvik.system.NativeStart.main(Native Method)

this is xml file that create items in my listView :

<ImageView
            android:id="@+id/imageFileView"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="9dp"
            android:layout_alignParentTop="true"/>
    <TextView
            android:layout_alignBottom="@+id/fileName"
            android:layout_width="97dp"
            android:layout_height="32dp"
            android:id="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="66dp"
            android:layout_alignParentRight="true"
            android:gravity="center_vertical"/>

and this is my listView :

<com.example.poc_cubbyhole.widgets.PullToRefreshListView
    android:id="@+id/list_files"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button_upload"
    android:layout_alignParentTop="true" 
    />

i use a widget to change add the pull and refresh to my listView

Upvotes: 0

Views: 858

Answers (3)

Thirumalvalavan
Thirumalvalavan

Reputation: 2768

I also faced like a problem even though my View id is correct. But finally I changed the id and clean the project then build once. It is worked correctly.

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14398

Change this

TextView textView = (TextView) rowView.findViewById(R.id.fileName);

to

TextView textView = (TextView) rowView.findViewById(R.id.textView);

in your getView method,Because your TextView Id is textView not fileName

Upvotes: 3

WonderSoftwares
WonderSoftwares

Reputation: 2810

YOu may be get null value from position on array here:

        int pos = position+4;

        String name = Names[pos];

for imediate solution you can add something like this:

int pos = position+4;

String name = ""+Names[pos];

so you dont have null value and dont get nullpointer,.

now if you want proper solution of getting nul value then debug applicaiton and check the valus of array and name every time that executes. Good Luck..

Upvotes: 0

Related Questions