2011-08-14 24 views
0

我做了一個類,其中包含一些字符串和整數,在那個類中我做了一個函數來將類中的數據轉換爲可讀的字符串;類中的android函數拋出java.lang.NullPointerException

public String GetConditions() { 
     String BigString = null; 
     String eol = System.getProperty("line.separator"); 
     try { 
      BigString += "Depth: " + ci(Depth) + eol; 

and so on... 

因爲我必須轉換很多整數,我做了一個額外的函數來將一個整數轉換爲一個字符串;

public String ci(Integer i) { 
    // convert integer to string 
    if (i != null) { 
     String a = new Integer(i).toString(); 
    return a; 
    } else { 
    return "n/a"; 
    } 
} 

這將引發對return a一個NullPointerException例外。我對Java很陌生,這可能是一個noob問題...對不起,提前致謝!

回答

0

謝謝你們,但我發現這個問題,我試過的東西添加到這是「空」,這行的字符串:

String BigString = null; 
0

嘗試轉換Integer您傳遞給您的方法字符串,而不是實例化一個新的。

你可以這樣做直線前進,如:

String a = i.toString();

String a = Integer.toString(i.intValue());

+1

投'Integer'到'字符串'甚至不會編譯。 –

+0

教育評論:D –

+0

感謝球員,但我發現問題,我試圖添加一些字符串'null',這一行: String BigString = null; – Lectere

1

還有一個更簡單的轉換一個IntegerString方式:使用String#valueOf(int)

public String ci(Integer i) 
{ 
    return i == null ? "n/a" : String.valueOf(i); 
} 
相關問題