简体   繁体   中英

I'm having this Error:Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

I'm having This Problem....

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
        at com.nacxo.darshansoni.team_prediction.HalaplayMatches.onCreate(HalaplayMatches.java:56)
        at android.app.Activity.performCreate(Activity.java:6974)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3012) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:232) 
        at android.app.ActivityThread.main(ActivityThread.java:6802) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 

This Is My Halaplaymatches Class:

public class HalaplayMatches extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private String name;
    TextView descriptionhala,e_advicehala,visit;
    ImageView imageView,imageView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_halaplay_matches);

        Bundle bundle=getIntent().getExtras();
        name = bundle.getString("title");

        visit=findViewById(R.id.hmustVisit);
       // e_advicehala=findViewById(R.id.advicehalaplay);
        descriptionhala=findViewById(R.id.deschalaplay);

You can do like it this.Before assigning value to name check whether bundle contains your key "title"

 if (getIntent() != null) {
            if (getIntent().getExtras() != null) {
                Bundle bundle = getIntent().getExtras();
                if (bundle.containsKey("title")) {
                    if (!TextUtils.isEmpty(bundle.getString("title")) && bundle.getString("title") != "")
                        name = bundle.getString("title");
                }
            }
        }

To invoke a method on an object means to call a method that is on that object; for example, "Hello World".length() invokes the length method on the String with value "Hello World" .

As the error message says, bundle must be null on the line name = bundle.getString("title"); , assuming that's line 56.

If you check the documentation , this happens when no extras have been added to the intent with putExtras() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM