2

Hi in the below code value am adding values to account_name the after that using that adapter doing the set adapter.response is coming the server but values are not adding to the list and list is also displaying.

For displaying used the recyclerview to display the values .

can any one help me where did the mistake

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference
        at com.genworks.oppm.Adapter.MyAdapter.onCreateViewHolder(MyAdapter.java:32)
        at com.genworks.oppm.Adapter.MyAdapter.onCreateViewHolder(MyAdapter.java:19)

updated code:

Activity.java:

for(SyncBlocks syncBlocks1:syncBlocks){

                        String label=syncBlocks1.getLabel();

                        ArrayList<SynFields> synFields=syncBlocks1.getFields();

                        ArrayList<SynFields> jsonArray=syncBlocks1.getFields();

                        for(SynFields synFields1:synFields){

                            String name=synFields1.getName();

                            Object values = synFields1.getValue().toString();

                            try {

                                if (values instanceof JSONObject) {
                                    JSONObject jsonObject1 = new
                                            JSONObject(String.valueOf(synFields1.getValue()));
                                    String value=jsonObject1.getString("value");
                                }else if (values instanceof String) {
                                    //here, you get string

                                     String value =  synFields1.getValue().toString();
                                    //account_name.addAll(value);
                                    String value_names= String.valueOf(synFields1.getValue());
                                   // Log.e("account_name", String.valueOf(account_name.add(value)));
                                    account_name.add(value_names);

                                    //account_name.add(value);

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        myAdapter = new MyAdapter(getContext(),account_name);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
                        recyclerView.setAdapter(myAdapter);

                    }

MyAdapter.java:

   public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<String> mSynFields=new ArrayList<>();

    public MyAdapter(Context context, ArrayList<String> synFieldsArrayList) {
        mContext=context;
        mSynFields=synFieldsArrayList;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.fragment_account, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

       holder.name.setText(mSynFields.get(position));


    }

    @Override
    public int getItemCount() {

        return mSynFields.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView country, name, city;
        ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);

            country = (TextView) itemView.findViewById(R.id.headingText);
            name = (TextView) itemView.findViewById(R.id.subHeaderText);
            city = (TextView) itemView.findViewById(R.id.subHeadingText);
        }

    }
}
  • You first store your data information by looping in the list variable and outside of the loop first make sure your recyclerview is cast, then configure the layoutmanager and finally recyclerview the adapter. – Javad Shakouri Feb 03 '20 at 06:39
  • @JavadShakouri where i did the mistake – jyothi dadi Feb 03 '20 at 06:47
  • You called LayoutManager after you called the adapter You first need to set LayoutManager, then apply the adapter and set the code and remove it from the loop – Javad Shakouri Feb 03 '20 at 06:51
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ryan M Feb 03 '20 at 08:50

3 Answers3

2

You are getting this error because you invoke inflate method from a null reference(inflater).

You have to initialize your inflater like below.

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        inflater = LayoutInflater.from(parent.getContext()); // add this line.

        //or you can do like below
        // View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_account, parent, false);

        View view = inflater.inflate(R.layout.fragment_account, parent, false);

        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
0

Place the below lines outside the for loop with the entire list as the parameter for the adapter that has been built inside the loop:

myAdapter = new MyAdapter(getContext(),account_name));
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

In addition to the above, you are passing account_name which is of type ArrayList<String> whereas in your adapter, your constructor is expecting synFieldsArrayList which is of type ArrayList<SynFields>. Both need to be of the same type. Either change your adapter, or your activity to make sure both are of same type.
In your MyViewHolder constructor, add:

Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.fragment_account, parent, false);
omz1990
  • 557
  • 4
  • 10
0

change your code to

ArrayList<SynFields> account_name=new ArrayList();

String label=syncBlocks1.getLabel();

ArrayList<SynFields> synFields=syncBlocks1.getFields();

ArrayList<SynFields> jsonArray=syncBlocks1.getFields();

for(SynFields synFields1:synFields){

String name=synFields1.getName();

   Object values = synFields1.getValue();

      try {

          if (values instanceof JSONObject) {
            JSONObject jsonObject1 = new 
                 JSONObject(String.valueOf(synFields1.getValue()));
          }else if (values instanceof String) {
            //here, you get string
            String value = String.valueOf(synFields1.getValue());
            account_name.add(value);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

myAdapter = new MyAdapter(getContext(),account_name);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));    
recyclerView.setAdapter(myAdapter);