Home Android-Populate a spinner using asynctask by json response from the server (NullPointerException: Attempt to invoke virtual method ..)
Reply: 0

Android-Populate a spinner using asynctask by json response from the server (NullPointerException: Attempt to invoke virtual method ..)

user4739
1#
user4739 Published in April 22, 2018, 6:49 am

I am trying to populate a Spinner from a JSON response using an async task. I am getting the JSON object as a server response and the response is correct. Here it is. [{"category_id":1,"category_name":"History"},{"category_id":2,"category_name":"Sports"},{"category_id":3,"category_name":"Science"},{"category_id":4,"category_name":"gg"}]

I am calling an asynctask on the View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstranceState)

But I am getting a 'null object reference error' on HomeFragment activity launch.

Here is my SpinnerAdapter.class.

package com.example.quizapp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.quizapp.model.Category;

import java.util.List;

public class SpinnerAdapter extends BaseAdapter
{
    private LayoutInflater layoutInflater;
    private List<Category> categoryList;
    private Context context;

public SpinnerAdapter(Context context, List<Category> list){
    this.context = context;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.categoryList = list;
}

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

@Override
public Object getItem(int position) {
    return (Category)categoryList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder spinnerHolder;
    if(convertView == null){
        spinnerHolder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.category_spinner_text_layout, parent,false);
        spinnerHolder.spinnerItemList = (TextView) convertView.findViewById(R.id.catsSpinnerText);
        convertView.setTag(spinnerHolder);
    }else {
        spinnerHolder = (ViewHolder) convertView.getTag();
    }
    spinnerHolder.spinnerItemList.setText(categoryList.get(position).getCatName());
    return convertView;
}

class ViewHolder{
    TextView spinnerItemList;
}

}

This is the AsyncTask for populating the Spinner written in the HomeFragment.class.

public class CategoryLoadAsyncTask extends AsyncTask<Void, String, Category[]> {

    @Override
    protected void onPreExecute(){
        progressDialog = ProgressDialog.show(getActivity(), "Loading Game", "Please wait..");
    }

    @Override
    protected Category[] doInBackground(Void... params) {
        String json ="";
        Category[] categories = null;
        try {
            json = new RestClient().doGet(getString(R.string.category_url));
            categories = new Gson().fromJson(json, Category[].class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return categories;
    }

    @Override
    protected void onPostExecute(Category[] categories){
        progressDialog.dismiss();
        if (categories != null){
            SpinnerAdapter spinnerAdapter = new SpinnerAdapter(context, Arrays.asList(categories));
            catsSpinner.setAdapter(spinnerAdapter);
        }
    }
}

Here is the error log.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.quizapp, PID: 2381 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:90) at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:64) at android.os.AsyncTask.finish(AsyncTask.java:660) at android.os.AsyncTask.-wrap1(AsyncTask.java) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Can anybody help me with this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306676 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO