Reputation: 145
I keep getting a compile error on the return menuFont line it says there is no variable menuFont. Could someone please tell how to fix this.
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;
}
}
Upvotes: 10
Views: 69303
Reputation: 18492
menuFont
doesn't exist outside of the try
block, which is what your compiler's trying to tell you. Instead, declare it before the try
block and then try and assign a value to it inside the try
block:
Font menuFont;
try {
Font menuFont = ...
}
...
return menuFont;
Upvotes: 2
Reputation: 373
This is because menuFont doesn't exist within the scope of the getFont method. Variables defined outside your current scope/block (pair of curly braces) are available, whereas ones defined in a nested block are not. You can correct this one of two ways:
menuFont
to above the try
(within the scope of the getFont
). Because you're anticipating an exception in Font.createFont, leave that code inside the try
block.return
statement to inside the try
block.Please see the answers to Does finally always execute in Java? for more subtleties about try/catch/finally.
Upvotes: 3
Reputation: 3522
Declare it outside the try
block.
http://docs.oracle.com/javase/specs/jls/se5.0/html/statements.html#14.4.2
Upvotes: 0
Reputation: 21429
The menuFont
variable scope is inside the try block. Move the variable outside of it instead.
Something like:
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;
}
Be aware that in case of an exception, this method will return a null
font.
Upvotes: 4
Reputation: 4350
Generally I would do the following:
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!
}
And check for null
wherever you call this method.
Upvotes: 0
Reputation: 45493
That because of the scope of your menuFont
variable. You declare it inside the try
, which means it will not be accessible from anywhere but inside those two braces. If you want to be able to access it in the catch
, or anywhere outside the try
, change your code to something as follows:
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
Upvotes: 2
Reputation: 4117
you must declare the variable menuFont outside...
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;
}
}
Upvotes: 3
Reputation: 25269
The basic problem with your code is that the Font object is only in scope for the duration of the try block, so it's no longer available in your return statement at the end of the method. Two options:
Move the variable declaration outside the try block:
Font menuFont = null;
try {
menuFont = Font.createFont(...);
}
catch (...) {
}
return menuFont;
Or, do return Font.creatFont(...)
inside the try, thus avoiding the need for the variable in the first place (and, obviously, return null
at the end of the method).
Upvotes: 26