0

I've been struggling for no reason and i don't see why. Here is my java code :

public class SousRubrique extends RelativeLayout {
private View sousRubriqueView;
private TextView titleTextView;
public SousRubrique(Context context, AttributeSet attrs, int defStyle) {
    super(context);
    init(context);
}
public SousRubrique(Context context, AttributeSet attrs) {
    super(context);
    init(context);
}
public SousRubrique(Context context) {
    super(context);
    init(context);
}

// Initialization methods

    public void init(Context context){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setSousRubriqueView(inflater.inflate(R.layout.sousrubrique, this));

        bind();
    }
    public void bind(){
        setTitleTextView((TextView) sousRubriqueView.findViewById(R.id.sousRubriqueTitle));
    }
    // end

    public void setTitle(String title){
        titleTextView.setText(title);
    }
    public TextView getTitleTextView() {
        return titleTextView;
    }
    public void setTitleTextView(TextView titleTextView) {
        this.titleTextView = titleTextView;
    }
    public View getSousRubriqueView() {
        return sousRubriqueView;
    }
    public void setSousRubriqueView(View sousRubriqueView) {
        this.sousRubriqueView = sousRubriqueView;
    }
}

Here is my xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical" >

<TextView
    android:id="@+id/sousRubriqueTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sous-Rubrique"
    android:layout_centerInParent="true"
    android:textColor="@android:color/white" />

How come Eclipse tells me there is a problem of java null pointer on the bind method ?

2
  • I know the way i ask it is a little harsh, but i've been on it like an hour long. I'm editing. Commented Jan 3, 2013 at 16:14
  • 2
    What's wrong with this @DGomez? He posted the all the code, said the error, and asked why he's getting a null pointer exception. Seems reasonable to me... maybe it was different before an edit? Commented Jan 3, 2013 at 16:16

1 Answer 1

1

the sousRubriqueView object is never initialized. That's why you are getting the NPE

Sign up to request clarification or add additional context in comments.

3 Comments

but with setSousRubriqueView(inflater.inflate(R.layout.sousrubrique, this)); in all my other classes, it initializes.
Sorry, I was going to delete the answer. did you try to clean the project?
the root should be null. Instead of pass this to the inflater pass null: inflater.inflate(R.layout.sousrubrique, null)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.