Benz
Benz

Reputation: 237

nullpointerexception on getview custom adapter

I am getting this exception when i try to display all the information from database.

02-27 01:49:49.074: E/AndroidRuntime(1956): FATAL EXCEPTION: main
02-27 01:49:49.074: E/AndroidRuntime(1956): java.lang.NullPointerException
02-27 01:49:49.074: E/AndroidRuntime(1956):     at my.fyp.inticlassifieds.CustomAdapter.getView(CustomAdapter.java:86)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.ListView.onMeasure(ListView.java:1159)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.View.measure(View.java:15848)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.os.Handler.handleCallback(Handler.java:730)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.os.Looper.loop(Looper.java:137)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invoke(Method.java:525)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 01:49:49.074: E/AndroidRuntime(1956):     at dalvik.system.NativeStart.main(Native Method)

This is my custom adapter class:

public class CustomAdapter extends BaseAdapter {


private ArrayList<Products> m_productsList = null;
private LayoutInflater mInflater = null;

private class Row{
TextView mTextViewPID;
TextView mTextViewName;
TextView mTextViewPrice;
}


public CustomAdapter(Context context, ArrayList<Products> productsList){
this.mInflater = LayoutInflater.from(context);
this.m_productsList = productsList;

}


// --------------------------------------------------
// BaseAdapter Overrides
// --------------------------------------------------

@Override
public int getCount() {
int count = 0;

if((m_productsList !=null) && (m_productsList.size() >= 1)){

    count = m_productsList.size();

}

return count;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return m_productsList.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

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

Row theRow;

// If ConvertVIew is null
if(convertView == null){

    theRow = new Row();

    convertView = mInflater.inflate(R.layout.activity_display_requested, null);

    theRow.mTextViewPID = (TextView) convertView.findViewById(R.id.pid);
    theRow.mTextViewName = (TextView) convertView.findViewById(R.id.name);
    theRow.mTextViewPrice = (TextView) convertView.findViewById(R.id.budget);


    convertView.setTag(theRow);

}else{

    theRow = (Row) convertView.getTag();
}

theRow.mTextViewPID.setText(m_productsList.get(position).pid);
theRow.mTextViewName.setText(m_productsList.get(position).name);
theRow.mTextViewPrice.setText(m_productsList.get(position).price);

return convertView;
}
}

The line that throws this exception is:

theRow.mTextViewPID.setText(m_productsList.get(position).pid);

activity_display_requested xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

I am stuck in this problem for about 6 hours already.

Upvotes: 0

Views: 386

Answers (2)

Raghunandan
Raghunandan

Reputation: 133570

There are no TextView's in a activity_display_requested.xml.

You are inflating the wrong layout probably. You are inflating activity_display_requested.xml.

convertView = mInflater.inflate(R.layout.activity_display_requested, null);

findViewById looks for a view in the current inflated layout. If you have layout with textviews then inflate that layout and initialize views

Upvotes: 3

Yogendra
Yogendra

Reputation: 5288

use this

if((m_productsList !=null)){
count = m_productsList.size();

} in getCount() methode.hope it will work.

Upvotes: 0

Related Questions