0

I have little problem with my code. I can't use variable "font" when I loaded it.

try{
       font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(new File("Font/M2c Light.ttf"))).deriveFont(Font.PLAIN,24);
 }catch(Exception ex){
       ex.printStackTrace();
 }

and when i want to use it on button

Button.setFont(font);

and that error shows me:

variable font might not have been initialized
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • didn't you search the error ? – Madhawa Priyashantha Mar 16 '16 at 18:01
  • Do this: `Font font = null;` or choose some default `Font` instead of using `null`. (The reason for the error is that, since the assignment to `font` is in a `try` block, there's no guarantee that `font` will have a value.) You could also put `font = null` (or, better yet, some default `Font`) in the `catch` block. – DSlomer64 Mar 16 '16 at 18:31

1 Answers1

0

try this code. let me know if it works

try {
 GraphicsEnvironment ge = 
     GraphicsEnvironment.getLocalGraphicsEnvironment();
 ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));
} catch (IOException|FontFormatException e) {
 //Handle exception
}