Home > Mobile >  JPE: Attempt to invoke virtual method 'android.content.res.Resources' on a null object ref
JPE: Attempt to invoke virtual method 'android.content.res.Resources' on a null object ref

Time:11-03

Well well well, I have already read these various posts : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' and this What is a NullPointerException, and how do I fix it? and this java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources() and have already tried a lot of things. But my problem remains the same. Maybe I missed something, I'm new, I'm trying my best so be nice please.

I call a method of my SMS Class in my Fragment "DataSetFragment1"

Can someone help me please and thank in advance

Here is the code of my SMS class :

public class Sms extends AppCompatActivity {
    private String contact;
    private String content;
    private int type;
    private int read;
    private String date;

 public void addSMS()
 {
    List<Sms> mesSms= new ArrayList<>();
    InputStream is = getResources().openRawResource(R.raw.sms1);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8"))
    );
 }
}

Here is the code of my Fragment :

public class Dataset1Fragment extends Fragment {
 private void createOnClicBtnEnreg(){
        Sms mesSms = new Sms();
        mesSms.addSMS();
}
 @Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.findViewById(R.id.btnEnreg).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btnEnreg = view.findViewById(R.id.btnEnreg);

            Context myContext = getContext();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                RoleManager roleManager = myContext.getSystemService(RoleManager.class);
                // check if the app is having permission to be as default SMS app
                assert roleManager != null;
                boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
                if (isRoleAvailable){
                    // check whether your app is already holding the default SMS app role.
                    boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
                    if (!isRoleHeld){
                        Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                        startActivityForResult(roleRequestIntent,5);
                    }
                }
            } else {
                Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myContext.getPackageName());
                startActivityForResult(intent, 5);
            }

            if (hasPermissions(getContext(),Permissions)){
                createOnClicBtnEnreg();
                Toast.makeText(getContext(), "Insertion OK", Toast.LENGTH_SHORT).show();

            }
 }

Here is my logs :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.data.myApp, PID: 12080
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:93)
    at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
    at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:566)
    at com.example.testbackup.Sms.addSMS(Sms.java:36)
    at com.example.testbackup.ui.dataset1.Dataset1Fragment.createOnClicBtnEnreg(Dataset1Fragment.java:321)
    at com.example.testbackup.ui.dataset1.Dataset1Fragment.access$100(Dataset1Fragment.java:61)
    at com.example.testbackup.ui.dataset1.Dataset1Fragment$2.onClick(Dataset1Fragment.java:251)

EDIT : This code is good, having followed the response Hata and then Ivo Beckers

public class Sms{
private String contact;
private String contenu;
private int type;
private int read;
private String date;


public void addSMS(Context myContext)
{
    List<Sms> mesSms= new ArrayList<>();
    InputStream is = myContext.getResources().openRawResource(R.raw.sms1);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8"))
    );

CodePudding user response:

Modify addSMS as following:

public void addSMS(Context context) {
    List<Sms> mesSms= new ArrayList<>();
    InputStream is = context.getResources().openRawResource(R.raw.sms1);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8"))
    );
}

and pass Context instance when you use it from createOnClicBtnEnreg:

private void createOnClicBtnEnreg(){
    Sms mesSms = new Sms();
    mesSms.addSMS(getContext());
}

Note: this answer is only for the error shown. If the error is cleared, this Q&A should be closed even if another error rises and remains (which should be posted as another question).

CodePudding user response:

you can't just extend any class to AppCompatActivity.
I assume your SMS object is not an Activity. I think you just did that so it exposes the getResources() method in your class, but that's not how it works.

You need a way to access a Context object wherever you want to do getResources() and then call it on that object. @hata's answer gives an example of how to do that

  • Related