Aman Arora
Aman Arora

Reputation: 109

Attempt to invoke virtual method ' 'on a null object reference

I am getting the following error

Attempt to invoke virtual method 'void android.widget.StackView.setAdapter(android.widget.Adapter)' on a null object reference

on this line

stackView.setAdapter(adapter);

The complete fragment EventsFragment.java is

public class EventsFragment extends android.support.v4.app.Fragment {
    private StackView stackView;
    private  ArrayList<Stack_Items> list;
    TypedArray eventLogo ;
    String eventName[];

    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        eventLogo= getResources().obtainTypedArray(R.array.event_stack_icon);
        eventName = getResources().getStringArray(R.array.event_stack);
        stackView = (StackView) getActivity().findViewById(R.id.stackView1);
        list = new ArrayList<Stack_Items>();

        //Adding items to the list
        for (int i = 0; i < eventLogo.length(); i++) {
            list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i,-1)));
        }
        //Calling adapter and setting it over stackView
        Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list );
        stackView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return inflater.inflate(R.layout.events_layout, null);
    }
}

Stack_Adapter.java

public class Stack_Adapter extends BaseAdapter {

    ArrayList<Stack_Items> arrayList;
    LayoutInflater inflater;
    ViewHolder holder = null;

    public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
        this.arrayList = arrayList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Stack_Items getItem(int pos) {
        return arrayList.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int pos, View view, ViewGroup parent) {
        if (view == null) {
            view = inflater.inflate(R.layout.stack_layout, parent, false);
            holder = new ViewHolder();

            holder.text = (TextView) view.findViewById(R.id.textView1);
            holder.image = (ImageView) view.findViewById(R.id.imageView1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.text.setText(arrayList.get(pos).getName());
        holder.image.setBackgroundResource(arrayList.get(pos).getImage());

        return view;
    }

    public class ViewHolder {
        TextView text;
        ImageView image;
    }

}

Stack_Items

public class Stack_Items {
    String name;
    Integer image;

    public Stack_Items(String name, Integer image) {
        this.name = name;
        this.image = image;
    }

    public String getName() {
        return name;

    }

    public int getImage() {

        return image;
    }

}

Upvotes: 0

Views: 6113

Answers (5)

Wisam Atil
Wisam Atil

Reputation: 91

change container and false to null

Do this:

View rootView = inflater.inflate(R.layout.events, null);

Upvotes: 0

Jfreu
Jfreu

Reputation: 589

R.id.stackView1 is not found when you try to assign stackView, so it is null

Upvotes: 0

Shahar
Shahar

Reputation: 3692

You are doing:

stackView = (StackView) getActivity().findViewById(R.id.stackView1);

Your stackView is null. getActivity().findViewById returns null.

Why are you using getActivity()?

Where is the stackView? You should load it from the right xml.

as @Tauqir mentioned, you need to inflate the right xml like this:

View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;

Upvotes: 3

varunkr
varunkr

Reputation: 5542

Do this

View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview

You first need to inflate the layout and call findViewById() on the View it returns, thus getActivity().findViewById should be rootView.findViewByID.

There are some other issues with your code as well. Like getctivity().getApplicationContext, just getActivity() is fine.

Upvotes: 0

Mohammad Tauqir
Mohammad Tauqir

Reputation: 1815

Try this inside onCreateView

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.events_layout, null);

        -------------some codes ------------

        stackView = (StackView) view.findViewById(R.id.stackView1);

        -------------some codes ------------
        return view;

    }

Upvotes: 0

Related Questions