尝试在空对象引用上调用虚方法'void com.newflame.pro.app.AppController.addToRequestQueue(com.android.volley.Request)'

时间:2016-09-22 13:05:53

标签: android rest android-volley

我正在尝试使用volley multipart请求者,并且我收到以下错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.newflame.pro.app.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
  at com.newflame.pro.Networking.MultiPartRequester.multipart_volley_requester(MultiPartRequester.java:64)
  at com.newflame.pro.Networking.MultiPartRequester.<init>(MultiPartRequester.java:35)
  at com.newflame.pro.flame.LoginNew.sendDataToServer(LoginNew.java:313)
  at com.newflame.pro.flame.LoginNew.access$100(LoginNew.java:75)
  at com.newflame.pro.flame.LoginNew$2$1$1.callback(LoginNew.java:197)
  at com.androidquery.callback.BitmapAjaxCallback.checkCb(BitmapAjaxCallback.java:502)
  at com.androidquery.callback.BitmapAjaxCallback.callback(BitmapAjaxCallback.java:472)
  at com.androidquery.callback.BitmapAjaxCallback.callback(BitmapAjaxCallback.java:65)
  at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:499)
  at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1261)
  at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:986)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我的Appcontroler.java

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();
    private static double total=0;
    public static ArrayList<Menu> menuListGlobal = new ArrayList<Menu>();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    LruBitmapCache mLruBitmapCache;

    private static AppController mInstance;

    public static double getTotal() {
        return total;
    }

    public static void setTotal(double total) {
        AppController.total = total;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {

            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            getLruBitmapCache();
            mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
        }

        return this.mImageLoader;
    }

    public LruBitmapCache getLruBitmapCache() {
        if (mLruBitmapCache == null)
            mLruBitmapCache = new LruBitmapCache();
        return this.mLruBitmapCache;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        req.setShouldCache(false);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }
}

这是我的MultipartRequester.java

public class MultiPartRequester {

    Context activity;
    private AsyncTaskCompleteListener asyncTaskCompleteListener;
    int service_code;


    public MultiPartRequester(Context activity, Map<String, String> map, int service_code, AsyncTaskCompleteListener asyncTaskCompleteListener) {

        this.activity = activity;
        this.service_code = service_code;
        this.asyncTaskCompleteListener = asyncTaskCompleteListener;
        String url = map.get(Const.URL);
        map.remove(Const.URL);
        multipart_volley_requester(url, map);
    }

    void multipart_volley_requester(String url, Map<String, String> map) {

        MultipartRequest mr = new MultipartRequest(url, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Ashutosh", error.toString());
                String msg = "No network connection.Please check your internet";
                //AndyUtils.showLongToast(msg,activity);
                //AndyUtils.removeProgressDialog();
            }

        }, new Response.Listener<String>() {

            @Override
            public void onResponse(String response)
            {
                Log.d("MultipartResponse",response);
                asyncTaskCompleteListener.onTaskCompleted(response.toString(), service_code);
            }

        }, map);

        mr.setRetryPolicy(new DefaultRetryPolicy(Const.TIMEOUT,
                Const.MAX_RETRY,
                Const.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(mr);
    }
}

这是代码,我在ping多分区类

private void sendDataToServer() {

    Log.d("shan","Thread1Executing");

    Log.d("shanpic",name);
    Log.d("shanpic",sSocial_unique_id);

    HashMap<String, String> map = new HashMap<>();
    map.put(Const.URL, Constant.loginurl);

        map.put(Const.Params.SOCIAL_UNIQUE_ID, sSocial_unique_id);
        map.put(Const.Params.LOGIN_BY, Constant.FACEBOOK);


    map.put(Const.Params.USERNAME, name);

     //   map.put(Const.Params.DOB, dob);

   if(sEmailId!=""){

    map.put(Const.Params.EMAIL, sEmailId);
   }

    map.put(Const.Params.PICTURE, filePath);

    map.put(Const.Params.DEVICE_TYPE, Const.ANDROID);
    if(preferences.getString(PROPERTY_REG_ID, "")!="")
    {
        map.put(Const.Params.DEVICE_TOKEN, preferences.getString(PROPERTY_REG_ID, ""));
    }

    new MultiPartRequester(LoginNew.this, map, Const.ServiceCode.REGISTER, this);
}

2 个答案:

答案 0 :(得分:0)

您没有初始化mInstance变量。在mInstance方法中初始化getInstance

更改

public static synchronized AppController getInstance() {
    return mInstance;
}

要,

public static synchronized AppController getInstance() {
    if(mInstance == null){
        mInstance = new AppController();
    }
    return mInstance;
}

答案 1 :(得分:0)

您可能错过了使用android:name属性在标记中添加此单例类,以便在应用启动时自动执行该类。

enter image description here

有关详细信息,请访问:Android working with Volley Library

保持编码............:)