Alex Moreno
Alex Moreno

Reputation: 729

Uncaught handler NullPointerException on a custom ArrayAdapter

This is where I set my custom ArrayAdapter:

lv.setAdapter(new MatchArrayAdapter(this, android.R.layout.simple_list_item_1, g.dm.getMatchesOnTeam(teamId), getWindowManager().getDefaultDisplay().getWidth(),getWindowManager().getDefaultDisplay().getHeight()));

This is my MatchArrayAdapter:

public class MatchArrayAdapter extends ArrayAdapter<Match> {
private Match[] matches;
private int height;
private int width;
private Context context;

public MatchArrayAdapter(Context context, int textViewResourceId,
        Match[] objects, int width, int height) {
    super(context, textViewResourceId, objects);
    this.matches = objects;
    this.width = width;
    this.height = height;
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.matchlistview, null);
    }
    //Get all the views
    TextView opponent = (TextView) convertView.findViewById(R.id.MatchOpponent);
    TextView month = (TextView) convertView.findViewById(R.id.MatchMonth);
    TextView day = (TextView) convertView.findViewById(R.id.MatchDay);
    ImageView matchMoreInformation = (ImageView) convertView.findViewById(R.id.MatchMoreInformation);

    opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());
    LayoutParams lpo = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpo.setMargins((int)(width*0.15), (int)(height*0.04), 0, 0);
    opponent.setLayoutParams(lpo);

    month.setText(String.valueOf(matches[position].getDate().get(Calendar.MONTH)));
    LayoutParams lpm = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpm.setMargins((int)(width*0.02), 0, 0, 0);
    month.setLayoutParams(lpm);

    day.setText(String.valueOf(matches[position].getDate().get(Calendar.DAY_OF_MONTH)));
    LayoutParams lpd = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpd.setMargins((int)(width*0.07), (int)(height*0.05), 0, 0);
    day.setLayoutParams(lpd);

    //"Next" arrow, arrow for more information of the match
    matchMoreInformation.setImageResource(android.R.drawable.ic_media_play);
    LayoutParams lpmmi = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lpmmi.setMargins((int)(width*0.81), (int)(height*0.044), 0, 0); 
    matchMoreInformation.setLayoutParams(lpmmi);

    return convertView;
}

}

And I'm always getting the following errors:

07-26 13:47:24.034: ERROR/AndroidRuntime(625): Uncaught handler: thread main exiting due to uncaught exception
07-26 13:47:24.044: ERROR/AndroidRuntime(625): java.lang.NullPointerException
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at data.MatchArrayAdapter.getView(MatchArrayAdapter.java:41)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.makeAndAddView(ListView.java:1668)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillDown(ListView.java:637)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.fillFromTop(ListView.java:694)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.ListView.layoutChildren(ListView.java:1521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.View.layout(View.java:6830)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:123)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-26 13:47:24.044: ERROR/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)

I've been trying all I've found. But nothing fix it out.

Edit: The code of line 41 is:

opponent.setText("vs. "+matches[position].getOpponentTeam().getTeamName());

I've set a breakpoint in that line and, matches[position].getOpponentTeam().getTeamName() is not null.

2nd edit: This is the xml:

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

    <TextView
        android:id="@+id/MatchOpponent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
    <TextView
        android:id="@+id/MatchMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
    <TextView
        android:id="@+id/MatchDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
    <ImageView
        android:id="@+id/MatchMoreInformation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">
    </ImageView>
</RelativeLayout>

Upvotes: 0

Views: 758

Answers (2)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

Are you sure that 'MatchOpponent' control was defined in matchlistview layout and was not defined in any other layout? Maybe this control was not inflated successful.

Upvotes: 1

Caner
Caner

Reputation: 59168

There is only one error there(the rest is stacktrace)and it is on line 41 of your source inside getView method(I think the code you posted does not include import statements etc. so I cannot know exactly which line it is).

You are trying to use a null object.

EDIT: If matches[position].getOpponentTeam().getTeamName() is not null then are you sure that opponent is not null? And are you sure R.id.MatchOpponent is a TextView

Upvotes: 1

Related Questions