2016-09-22 76 views
0

我試圖使用抽頭多部分請求者,並且我得到以下錯誤。嘗試在空對象引用上調用虛擬方法'void com.newflame.pro.app.AppController.addToRequestQueue(com.android.volley.Request)'

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); 
    } 
} 

這是代碼,其中進出口查驗multipartrequester類

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); 
} 
+0

請人指導我,我正在做一個地方錯誤。 –

+0

在AppController類中,在onCreate()方法中使用'mInstance = this;'。 – Piyush

回答

0

你是不初始化mInstance變量。在getInstance方法中初始化mInstance。 name屬性自動執行類,每當應用程序啓動:

變化

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

要,

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