Android Volley Null Pointer Exception

I’m attempting to convert a previously Activity-based tutorial to a Fragment, but I continue to run into the NullPointerException error on my Adapter.

The tutorial is based on this and I have slimmed down the constructor for the Adapter because it used to be calling an Activity.

  • Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/index.html
  • Google places API returning only 5 results
  • Android XML Error parsing XML: junk after document element
  • How do I link my extension to my Android app?
  • Difference between Intent.ACTION_GET_CONTENT and Intent.ACTION_PICK
  • Displaying More string on Logcat
  • There is one possible solution here but nobody knows how to proceed with the same possible question.

    I want to convert everything to a working Fragment. Please let me know if you need more information from me.

    Main converted class:

    public class mainViewController2 extends Fragment {
        private static final String TAG = mainViewController2.class.getSimpleName();
        private ListView listView;
        private FeedListAdapter listAdapter;
        private List<FeedItem> feedItems;
        private String URL_FEED = "";
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.mainviewcontroller2_fragment, container, false);
            listView = (ListView) v.findViewById(;
            feedItems = new ArrayList<FeedItem>();
            //where the error shows up?
            listAdapter = new FeedListAdapter(feedItems);
            // We first check for cached request
            Cache cache = AppController.getInstance().getRequestQueue().getCache();
            Entry entry = cache.get(URL_FEED);
            if (entry != null) {
                // fetch the data from cache
                try {
                    String data = new String(, "UTF-8");
                    try {
                        parseJsonFeed(new JSONObject(data));
                    } catch (JSONException e) {
                } catch (UnsupportedEncodingException e) {
            } else {
                // making fresh volley request and getting json
                JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                        URL_FEED, null, new Response.Listener<JSONObject>() {
                            public void onResponse(JSONObject response) {
                                VolleyLog.d(TAG, "Response: " + response.toString());
                                if (response != null) {
                        }, new Response.ErrorListener() {
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d(TAG, "Error: " + error.getMessage());
                // Adding request to volley request queue
            return v;
         * Parsing json reponse and passing the data to feed view list adapter
         * */
        private void parseJsonFeed(JSONObject response) {
            try {
                JSONArray feedArray = response.getJSONArray("feed");
                for (int i = 0; i < feedArray.length(); i++) {
                    JSONObject feedObj = (JSONObject) feedArray.get(i);
                    FeedItem item = new FeedItem();
                    // Image might be null sometimes
                    String image = feedObj.isNull("image") ? null : feedObj
                    // url might be null sometimes
                    String feedUrl = feedObj.isNull("url") ? null : feedObj
                // notify data changes to list adapater
            } catch (JSONException e) {


     public class FeedListAdapter extends BaseAdapter { 
        private Activity activity;
        private LayoutInflater inflater;
        private List<FeedItem> feedItems;
        Context context;
        ImageLoader imageLoader;
         public FeedListAdapter(Context ctx,List<FeedItem> feedItems) {
            this.context= ctx;
            this.feedItems = feedItems;
            imageLoader = AppController.getInstance().getImageLoader();
            inflater = LayoutInflater.from(context);
        public int getCount() {
            return feedItems.size();
        public Object getItem(int location) {
            return feedItems.get(location);
            public long getItemId(int position) {
                return position;
            public View getView(int position, View convertView, ViewGroup parent) {
                if (inflater == null)
                    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (convertView == null)
                    convertView = inflater.inflate(R.layout.feed_item, null);
                if (imageLoader == null)
                    imageLoader = AppController.getInstance().getImageLoader();
                TextView name = (TextView) convertView.findViewById(;
                TextView timestamp = (TextView) convertView
                TextView statusMsg = (TextView) convertView
                TextView url = (TextView) convertView.findViewById(;
                NetworkImageView profilePic = (NetworkImageView) convertView
                FeedImageView feedImageView = (FeedImageView) convertView
                FeedItem item = feedItems.get(position);
                // Converting timestamp into x ago format
                CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                        System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
                // Chcek for empty status message
                if (!TextUtils.isEmpty(item.getStatus())) {
                } else {
                    // status is empty, remove from view
                // Checking for null feed url
                if (item.getUrl() != null) {
                    url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                            + item.getUrl() + "</a> "));
                    // Making url clickable
                } else {
                    // url is null, remove from the view
                // user profile pic
                profilePic.setImageUrl(item.getProfilePic(), imageLoader);
                // Feed image
                if (item.getImge() != null) {
                    feedImageView.setImageUrl(item.getImge(), imageLoader);
                            .setResponseObserver(new FeedImageView.ResponseObserver() {
                                public void onError() {
                                public void onSuccess() {
                } else {
                return convertView;

    LogCat errors:

    09-06 02:51:55.823: E/AndroidRuntime(8279): FATAL EXCEPTION: main
    09-06 02:51:55.823: E/AndroidRuntime(8279): Process: com.rynovation.kline, PID: 8279
    09-06 02:51:55.823: E/AndroidRuntime(8279): java.lang.NullPointerException
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at androidFeedClasses.FeedListAdapter.<init>(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at com.rynovation.kline.mainViewController2.onCreateView(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at$
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at android.os.Handler.handleCallback(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at android.os.Handler.dispatchMessage(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at android.os.Looper.loop(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at java.lang.reflect.Method.invoke(
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at$
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at
    09-06 02:51:55.823: E/AndroidRuntime(8279):     at dalvik.system.NativeStart.main(Native Method)

    Related posts:

    Android Preferences error, &ldquo;String cannot be cast to int&rdquo;
    Android Studio doesn&#039;t display logs by package name
    Android Fragment - move from one View to another?
    Android back button and MediaController
    Youtube SDK crashes with DeadObjectException when move to youtube app
    Granting Android application the android.permission.INTERNET permission while debugging on device
  • Integrating AndroidAnnotation with Intellij IDEA 12
  • Where does Eclipse hide the keystore?
  • Android Studio Git Push Rejected
  • Error running Android (written in Kotlin) project compiled with Gradle2.0.0-alpha plugin
  • Lucene in Android
  • lock/unlock orientation
  • 5 Solutions collect form web for “Android Volley Null Pointer Exception”

    I guess you missed a simple code in your manifest file. Your AppController package extends Application , So the application tag in your manifest is searching for the AppController but isn’t able to find.

    Just Write this simple code in your AndroidManifest.xml

    android:name = your.package.AppController

    A little late maybe, but a representative answer to user3902144’s answer:

    Similar Answer to this question : Link

    You may have missed out this from your manifest:

            android:name="<package-name>.app.AppController" // <-- this one

    That’s why onCreate never called and mInstance is null.

    Hope this helps 🙂

    Looking at the error, it seems like you’re calling getActivity() inside onCreateView(). You should be getting the activity in onActivityCreated instead where it shouldn’t be null.

    Change this from

    listView = (ListView) getActivity().findViewById(;


    listView = (ListView)v.findViewById(;

    This is because getActivity() is generally used for as a Context in Fragment. While you’r find id for your UI elements in Fragment then you have to give reference of your View's object which will be returned in onCreateView() method.


    Change your Adapter Like,

    listAdapter = new FeedListAdapter(getActivity(),feedItems);

    Now in its Constructor

     ImageLoader imageLoader;
     public FeedListAdapter(Context ctx,List<FeedItem> feedItems) {
        this.context= ctx;
        this.feedItems = feedItems;
        imageLoader = AppController.getInstance().getImageLoader()

    Now in your adapter class use context variable as a Context.

    Please check all your params carefully, make sure not to send any null value in params, if using some stored values then use the condition like below:

    protected Map<String, String> getParams() {
     Map<String, String> params = new HashMap<String, String>();
     if(paramValue!= null)
         params.put("paramKey", paramValue);
         params.put("paramKey", "anyOtherDefaultValue");
     return params;
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.