2012-03-04 74 views
4

我不斷收到一個編譯錯誤在返回menuFont行它說沒有變量menuFont。有人可以告訴如何解決這個問題。嘗試捕獲內存訪問變量

import java.awt.Font; 
import java.awt.FontFormatException; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 


public class loadFiles { 
Font getFont(){ 
      try { 
       Font menuFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); 

      } catch (FileNotFoundException e) { 
       System.out.println("Cant find file."); 
       e.printStackTrace(); 
      } catch (FontFormatException e) { 
       System.out.println("Wrong file type."); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       System.out.println("Unknown error."); 
       e.printStackTrace(); 
      } 
      return menuFont; 
    } 
} 

回答

14

與您的代碼的基本問題是Font對象只是餘地try塊的持續時間,因此它不再在您所在return語句在方法結束。有兩個選項:

移動try塊外面的變量聲明:

Font menuFont = null; 
try { 
    menuFont = Font.createFont(...); 
} 
catch (...) { 

} 
return menuFont; 

或者說,return Font.creatFont(...)的嘗試中,從而避免了在第一位的變量的需求(而且,很顯然,return null在方法的結尾)。

+0

非常感謝你。 – twilding 2012-03-04 00:22:27

2

menuFonttry塊以外不存在,這是您的編譯器試圖告訴您的內容。相反,try塊之前聲明它,然後嘗試和值分配給它的try塊內:

Font menuFont; 

try { 
    Font menuFont = ... 
} 
... 

return menuFont; 
3

必須聲明變量menuFont外...

import java.awt.Font; 
import java.awt.FontFormatException; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class loadFiles { 
    Font getFont(){ 
     Font menuFont = null; 
     try { 
      menuFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); 

     } catch (FileNotFoundException e) { 
      System.out.println("Cant find file."); 
      e.printStackTrace(); 
     } catch (FontFormatException e) { 
      System.out.println("Wrong file type."); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.println("Unknown error."); 
      e.printStackTrace(); 
     } 
     return menuFont; 
    } 

} 
2

這因爲變量範圍爲menuFont。你在try之內聲明它,這意味着它不能從任何地方訪問,但在這兩個括號內。如果你希望能夠訪問它在catch,或try以外的任何地方,如下代碼更改爲:

Font menuFont = null; 
try { 
    menuFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); 
} catch (FileNotFoundException e) { 
    // you can access menuFont here (too) 
} 
//... 
return menuFont; // <-- note: can be null here 
0

一般來說,我會做到以下幾點:

import java.awt.Font; 
import java.awt.FontFormatException; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 


public class loadFiles { 
Font menuFont = null; 
Font getFont(){ 
      try { 
       menuFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); 

      } catch (FileNotFoundException e) { 
       System.out.println("Cant find file."); 
       e.printStackTrace(); 
      } catch (FontFormatException e) { 
       System.out.println("Wrong file type."); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       System.out.println("Unknown error."); 
       e.printStackTrace(); 
      } 
    } 
    return menuFont; // could potentially be null! 
} 

,並檢查對於null,無論你調用此方法。

3

變量作用域的menuFont位於try塊內。改爲將變量移到它外面。 喜歡的東西:

Font getFont(){ 
     Font menuFont = null; 
     try { 
      menuFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); 

     } catch (FileNotFoundException e) { 
      System.out.println("Cant find file."); 
      e.printStackTrace(); 
     } catch (FontFormatException e) { 
      System.out.println("Wrong file type."); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.println("Unknown error."); 
      e.printStackTrace(); 
     } 
     return menuFont; 
    } 

要知道,在出現異常的情況下,此方法將返回一個null字體。

3

這是因爲menuFont不所述的getFont方法的範圍之內。在當前作用域/塊(花括號對)之外定義的變量可用,而在嵌套塊中定義的變量不可用。您可以矯正的兩種方式這一個:

  • 移動的menuFont該項聲明try上方(getFont的範圍之內)。由於您正在預測Font.createFont中的異常,請將該代碼保留在try塊內。
  • return語句移動到try塊內。

請參閱Does finally always execute in Java?的答案以獲得更多關於try/catch/finally的更多細節。