0

I have a library that I use in my application. In my library, I need to get a resource ( a bks file). I don't have a main activity in my library. How do I get a resource without an activity. Here is the code I have.

public class PostRequest {
    Context context;
    MyApplication application;

 public String post(){
    KeyStore trustStore = KeyStore.getInstance("BKS");
    InputStream trustStoreStream = application.getResources().openRawResource(R.raw.certificate);
}
}

I am getting the error, Attempt to invoke virtual method getResources() on a null object reference.

I created a variable Context context to and used that context.getResources().openRawResource(R.raw.certificate); but still with no success. This is a library so I don't have MainActivity or any activity classes.

winfred adrah
  • 428
  • 6
  • 18

2 Answers2

0

First of all, context and application instance variables are just references there.

You haven't assigned the references yet so you can't use their method calls.

In analogous to an empty envelope, you still haven't prepared a letter so you can't read or write it yet.


Second, assuming that MyApplication is extended from Application and also it is located in the library module as a standalone component, I recommend to remove it since Application class defined in the application using the library shall be used instead of the library one.


IMO, if you are developing a library and require to use context, just define it as a contsructor parameter or a method parameter and let the caller provides to you.

public class PostRequest {
    Context context;
    MyApplication application;

 // Constructor to ask for context
 public PostRequest(Context _context) {
   this.context = _context;
 }

 // Method signature to ask for context
 public String post(Context context){
    KeyStore trustStore = KeyStore.getInstance("BKS");
    InputStream trustStoreStream = context.getResources().openRawResource(R.raw.certificate);
}
}

OR if you ensure that your MyApplication class defined can be used by the library, just define a static context variable there and use it (i.e. MyApplication.context). Just remember to use getApplicationContext() there to avoid resources leakage.

REF: Static way to get 'Context' in Android?

  • I followed this and still getting the same error. – winfred adrah Nov 07 '22 at 10:34
  • May I ask how you change the code? – Android Newbie A Nov 07 '22 at 10:39
  • Also, how you use the method gives me more hints. I guess that the context args defined in App module & passing to the method call is also null. So where is the origin of the context? – Android Newbie A Nov 07 '22 at 10:43
  • `String xmlRecords = postRequest.post(context);` When calling the method in another class in a method, I pass a context as a parameter. This context is not declared anywhere. – winfred adrah Nov 07 '22 at 10:52
  • I bet the context passing in to the post method is still null. May I ask what kind of class you are calling it from? Let's try getApplicationContext() first. – Android Newbie A Nov 07 '22 at 10:55
  • If the class, let's say Class A cannot call getApplicationContext(). Then you shall find the previous caller and pass the context to class A. Let's say Activity B -> Class A -> postRequest.post method, then the Context var shall origin from Activity B – Android Newbie A Nov 07 '22 at 10:57
  • Actually debugger is our good friend. Just try to call Log.e("Test", context) to verify the context passing is not null first. – Android Newbie A Nov 07 '22 at 10:59
  • Newbie, I am now getting `Can't create handler inside thread that has not called Looper.prepare()`, I am not sure how that applies to my current problem – winfred adrah Nov 07 '22 at 12:19
  • I guess you are doing an action which requires a UI thread but now you are in a worker thread. Better to open a new ticket for this since you are asking why you get the error "Attempt to invoke virtual method getResources() on a null object reference." – Android Newbie A Nov 08 '22 at 01:40
  • Or you may give a try by yourself. Keywords are your error message, "runOnUiThread", "android ui thread vs worker thread". One more hints, if you turnout want to use runOnUiThread directly, it will not work for getApplicationContext() and you will need to have the activity context to do so. – Android Newbie A Nov 08 '22 at 01:45
0

add constructor to your class:

public class PostRequest {
    Context context;
    MyApplication application;

    public  PostRequest(Context context,MyApplication application){
        this.context = context;
        this.application = application;;
    }

    public String post(){
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream trustStoreStream = context.getResources().openRawResource(R.raw.certificate);
    }
}