2

I'm getting a NullPointerException on my ArrayAdapter. I'm getting a value from a database connected to the localhost and When I post the value to the log cat and delete the textview.settext("something") my error is gone. But when I initialize the TextView and put something in it the error shows again.

Here is my snippet:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final List<String> values;
    //private final List<String> arr;

    public MySimpleArrayAdapter(Context context, List<String> values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    static class ViewHolder {
        public TextView text;
        public ImageView image;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("List",values.get(position).toString());
        LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder viewHolder = new ViewHolder();
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.txtlarge);
        textView.setText("hahhaha");

        return rowView;
    }

    private Drawable LoadImageFromWebOperations(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } 
        catch (Exception e)
        {
            Log.w("LoadImageFromWebOperations",e.toString());
            return null;
        }
    }
}

My error is here:

07-09 02:20:47.433: E/AndroidRuntime(1168): FATAL EXCEPTION: main
07-09 02:20:47.433: E/AndroidRuntime(1168): java.lang.NullPointerException
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.database_demo.MySimpleArrayAdapter.getView(MySimpleArrayAdapter.java:57)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.fillDown(ListView.java:652)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.fillFromTop(ListView.java:709)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.ListView.layoutChildren(ListView.java:1580)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.View.layout(View.java:7035)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.os.Looper.loop(Looper.java:123)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 02:20:47.433: E/AndroidRuntime(1168):     at dalvik.system.NativeStart.main(Native Method)

Please help thanks

2
  • what is line 57 in MySimpleArrayAdapter.java ? Commented Jul 9, 2012 at 7:34
  • Sorry guys. Im so stupid.. hahaha.. I finaly got it.. I changed this View rowView = inflater.inflate(R.layout.rowlayout, parent, false); to this: View rowView = inflater.inflate(R.layout.listinflate,null); Im always confuse of using inflater.. the listinflate file contains the textview while the rowlayout contains the listview. Thats why i got nullpointer cause i use rowlayout.. Thanks to [Vipul Shah][1] i change the parent,false to null.. :) Commented Jul 9, 2012 at 7:47

4 Answers 4

4

Replace

View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

By

View rowView = inflater.inflate(R.layout.rowlayout, null);
Sign up to request clarification or add additional context in comments.

3 Comments

@thenewbie Rest of the code looks fine in getView.The last thing i would like you to check is if R.id.txtlarge is present in rowlayout
AHm yeah hahaha my bad i use Rowlayout. But the textview is in the listinflate.xml not rowlayout.xml.. haha I always got them mix up.. Thanks..
I cant hahaha I would like to answer my own question but it will take 8hrs.. :)
0

First you are Using viewholder in a bad way, Second use convertView = inflater.inflate(R.layout.rowlayout, null);

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

if (convertView ==null ) {

    convertView = inflater.inflate(R.layout.rowlayout, null);
    // Creates a ViewHolder and store references to the two children views
    // we want to bind data to.
    holder = new ViewHolder();
    convertView.setTag(holder);
} else {
    // Get the ViewHolder back to get fast access to the TextView
    // and the ImageView.
    holder = (ViewHolder) convertView.getTag();
}

// Bind the data efficiently with the holder.
holder.text = (TextView) convertView.findViewById(R.id.txtLarge);
holder.image = (ImageView) convertView.findViewById(R.id.icon);

3 Comments

why not just bind the View when convertView is NULL ? Outside of if-else block you can call directly to the View objects. I don't think always try to call findViewById is a good idea, cause the search View algorithm quite takes time.
i saw that code already in the some blog some time. Did you copy it?? hehe Peace :)
@thenewbie I haven't copy it Sir, this is how Design Pattern ViewHolder works, aand stop making comments like these!! It's not funny
0

There's nothing wrong on your above code in order to run normally. Based on many empty lines, I guess you have tried to deleted some lines, update layout ..blah blah. So I think clean project and re-run it again.

Sometimes, when you try to edit layout and don't clean your projects, it will use the previous build cache, which usually cause NullPointerException because the new layout is not updated.

1 Comment

Yeah i delete some code.. I figure out what causing my problem. But thanks anyway. I didnt know that not cleaning the projects can make a null pointer hahaha.. Thanks for the tip
0

Most probably, your textview or rowview is a null object :-)

Have you tried debugging it ?

EDIT

1 Comment

At least, you should know which one is null which gives ground for investigation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.