Sunday, 15 September 2013

java - How to check for null on an object and then move on if it is NULL? -



java - How to check for null on an object and then move on if it is NULL? -

in java, best way check if object has value or returning null? examples have found not good. have code:

mdbapi.getsession().setaccesstokenpair(reauthtokens); system.out.println(reauthtokens); if(reauthtokens.equals(null)) { mdbapi.getsession().startauthentication(main.this); log.e(tag, "keys not set -- i'm starting authentication"); }

i'm trying reauthtokens checked value , if has none, move on , authenticate. however, nullpointerexception on if statement line. there can better?

____________oncreate rcook________________________

@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().requestfeature(window.feature_no_title); setcontentview(r.layout.main); //accesstokenpair tokens=null; //accesstokenpair tokens = getstoredkeys(); //system.out.println(access + "here am"); //clearkeys(); //log.e(tag, "keys cleared"); appkeypair appkeys = new appkeypair(app_key, app_secret); androidauthsession session = new androidauthsession(appkeys, access_type); mdbapi = new dropboxapi<androidauthsession>(session); accesstokenpair reauthtokens = new accesstokenpair(app_key, app_secret); mdbapi.getsession().setaccesstokenpair(reauthtokens); system.out.println(reauthtokens); if(reauthtokens == null) { mdbapi.getsession().startauthentication(main.this); log.e(tag, "keys not set -- i'm starting authentication"); } // /*read settings*/ msettings = getsharedpreferences(prefs_name, 0); boolean hide = msettings.getboolean(prefs_hidden, false); boolean thumb = msettings.getboolean(prefs_thumbnail, true); int space = msettings.getint(prefs_storage, view.visible); int color = msettings.getint(prefs_color, -1); int sort = msettings.getint(prefs_sort, 3); mfilemag = new filemanager(); mfilemag.setshowhiddenfiles(true); mfilemag.setsorttype(sort); if (savedinstancestate != null) mhandler = new eventhandler(main.this, mfilemag, savedinstancestate.getstring("location")); else mhandler = new eventhandler(main.this, mfilemag); mhandler.settextcolor(color); mhandler.setshowthumbnails(thumb); mtable = mhandler.new tablerow(); /*sets listadapter our listactivity , *gives our eventhandler class same adapter */ mhandler.setlistadapter(mtable); setlistadapter(mtable); /* register context menu our list view */ registerforcontextmenu(getlistview()); mstoragelabel = (textview)findviewbyid(r.id.storage_label); mdetaillabel = (textview)findviewbyid(r.id.detail_label); mpathlabel = (textview)findviewbyid(r.id.path_label); mpathlabel.settext("path: /sdcard"); updatestoragelabel(); mstoragelabel.setvisibility(space); mhandler.setupdatelabels(mpathlabel, mdetaillabel); /* setup buttons */ int[] img_button_id = {r.id.help_button, r.id.home_button, r.id.back_button, r.id.info_button, r.id.manage_button, r.id.multiselect_button, r.id.dropbox_button }; int[] button_id = {r.id.hidden_copy, r.id.hidden_attach, r.id.hidden_delete, r.id.hidden_move}; imagebutton[] bimg = new imagebutton[img_button_id.length]; button[] bt = new button[button_id.length]; for(int = 0; < img_button_id.length; i++) { bimg[i] = (imagebutton)findviewbyid(img_button_id[i]); bimg[i].setonclicklistener(mhandler); if(i < 4) { bt[i] = (button)findviewbyid(button_id[i]); bt[i].setonclicklistener(mhandler); } } intent intent = getintent(); if(intent.getaction().equals(intent.action_get_content)) { bimg[5].setvisibility(view.gone); mreturnintent = true; } else if (intent.getaction().equals(action_widget)) { log.e("main", "widget action, string = " + intent.getextras().getstring("folder")); mhandler.updatedirectory(mfilemag.getnextdir(intent.getextras().getstring("folder"), true)); } }

use if (reauthtokens == null)) instead. you're not trying compare contents of objects; you're trying compare references. "is reauthtokens points same address null?"

edit next updates op: reauthtokens of type accesstokenpair (and i'm betting many readers thought list... know did). instantiated in line:

accesstokenpair reauthtokens = new accesstokenpair(app_key, app_secret);

which why next status false: reauthtokens == null. that's why, when coding if (reauthtokens == null), "dead code" warning: compiler knows status can never true, instantiate reauthtokens few lines above.

so, type of comparing you're after not reference, content. want check whether reauthtokens "empty". doesn't create sense code had quoted. how come instantiate object, , want check if it's "empty"?

i think logic isn't right. should first obtain access token pair expect (the session?), , compare result null. this:

accesstokenpair reauthtokens = mdbapi.getsession().getaccesstokenpair(); if (reauthtokens == null) { reauthtokens = new accesstokenpair(...); mdbapi.getsession().setaccesstokenpair(reauthtokens); }

java if-statement nullpointerexception

No comments:

Post a Comment