2013-02-21 46 views
1

在Java中,檢查對象是否有價值或正在返回的最佳方法是什麼?null?我發現的大多數例子都不是很好。基本上,我有這樣的代碼:如何檢查對象上的null,然後繼續移動,如果它爲NULL?

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"); 
} 

我試圖讓reAuthTokens要檢查的價值,如果它現在沒有,繼續前進和驗證。不過,我只在if語句行上獲得NullPointerException。有什麼我可以做得更好嗎?

__ _ __ _ __ _ __ _的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 I 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 the ListAdapter for our ListActivity and 
    *gives our EventHandler class the same adapter 
    */ 
    mHandler.setListAdapter(mTable); 
    setListAdapter(mTable); 

    /* register context menu for 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 i = 0; i < 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)); 

    } 
} 

回答

5

使用if (reAuthTokens == null))代替。你並沒有試圖比較對象的內容;你正在試圖比較參考。 「是reAuthTokens指向與null相同的地址嗎?」

編輯以下從OP更新:reAuthTokensAccessTokenPair型(和我打賭許多讀者最初以爲這是一個List ......我知道我做了)。它被實例化在這一行:

AccessTokenPair reAuthTokens = new AccessTokenPair(APP_KEY, APP_SECRET); 

這就是爲什麼以下條件將永遠是假的:reAuthTokens == null。這就是爲什麼當編碼爲if (reAuthTokens == null)時,你會得到一個「死代碼」的警告:編譯器知道這種情況永遠不會是真的,因爲你在上面幾行實例化reAuthTokens

因此,您所追求的比較類型不是關於參考,而是關於內容。你想檢查reAuthTokens是否爲「空」。但是從你引用的代碼來看,這沒有任何意義。你如何實例化對象,然後想檢查它是否爲「空」?

我認爲你的邏輯是不正確的。您應該首先從您期望的位置(會話?)獲取訪問令牌對,然後將結果與null進行比較。這樣的事情:

AccessTokenPair reAuthTokens = mDBApi.getSession().getAccessTokenPair(); 

if (reAuthTokens == null) { 
    reAuthTokens = new AccessTokenPair(...); 
    mDBApi.getSession().setAccessTokenPair(reAuthTokens); 
} 
+0

請參閱我試過,但如果我這樣做,Eclipse會將語句中的代碼報告爲「死代碼」。我知道reAuthTokens也有價值。 – 2013-02-21 02:03:40

+1

@SteveWeaver你確定嗎?如果您在將'reAuthTokens'與'null'比較之前再次檢查了值,請再次檢查。 – 2013-02-21 02:08:37

+0

是肯定的......它從System.out.println(reAuthTokens)中吐出它在LogCat中; - 它的值是{key =「uhvcudhc8r8f」,secret =「ufur8u8r9euf4」} – 2013-02-21 02:11:16