2012-03-04 73 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的更多细节。