#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Posts
    2
    Rep Power
    0

    Question NullPointerException help


    hi
    I have two separate apps working fine
    I wanted to merge them, so copied the classes and layouts of the second one to the first one.
    there were no errors till I run the app and found that there is a NullPointerException
    I dont know why did it happen because it wasnt in the last codes.
    I can give you the codes if you can help me
    please help
    thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Posts
    2
    Rep Power
    0
    here is the code that it says has the error:

    Code:
        public class AppController extends Application {
    
            public static final String TAG = AppController.class.getSimpleName();
    
            private RequestQueue mRequestQueue;
    
            private static AppController mInstance;
    
            @Override
            public void onCreate() {
                super.onCreate();
                mInstance = this;
            }
    
            public static synchronized AppController getInstance() {
                return mInstance;
            }
    
            public RequestQueue getRequestQueue() {
                if (mRequestQueue == null) {
                    mRequestQueue = Volley.newRequestQueue(getApplicationContext());
                }
    
                return mRequestQueue;
            }
    
            public  void addToRequestQueue(Request req, String tag) {
                if(tag!=null){
                    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
                    getRequestQueue().add(req);
                }
            }
        }
    in

    Code:
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2016
    Posts
    2
    Rep Power
    0
    Running the debugger in android studio is the way to find a null pointer like this. You can filter what gets debugged or skipped so you don't wonder in the wild blue yonder of library code etc.

    As for creating objects in Android it is not using constructors and a "new" keyword or get instance in this case that will make a new object. For example an activity instance must resolve the language of your device, the proper icon size for your device screen Font size. You must use callbacks like oncreate. I noticed Application does have a no argument constructor.

    On to your code. I can only make assumptions because you did not include the whole project. AppController on create method is not called implicitly or explicitly therefore Application context is null or garbage

IMN logo majestic logo threadwatch logo seochat tools logo