Hello
I am trying to write to a text log file for my world of zuul game. I have created a Class Logger to write into the file, here is the code:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Logger
{
private BufferedWriter out;
public Logger() throws IOException
{
//
fw = new FileWriter("logfile.txt");
//
bw = new BufferedWriter(fw);
}
public void logToFile(String input) throws IOException
{
this.input = input;
bw.write("User Input: " + input + "\n");
}
public void closeStream()
{
bw.close();
}
}
in the main Game class, I have added this bit to write into the file:
public void play()
{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
String input = command.getCommandWord() + command.getSecondWord();
logger.logToFile(input); //i get the error in here!
finished = processCommand(command);
}
logger.closeStream();
System.out.println("Thank you for playing. Good bye.");
}
and when compiling i got this error: unreported exception java.io.exception must be caught or declared to be thrown.
Not sure what it means! any help would be much appreciated!
mumaga
0
Light Poster
Recommended Answers
Jump to PostExceptions which are not RuntimeExceptions must be handled explicitly. They cannot be ignored. A method where an Exception may occur can either declare it to be thrown, or catch it. You catch an exception by using something like:
try { methodThatMightThrowExceptionA(); } catch(ExceptionA e) { whatIWantToDoIfExceptionAHappens(); } …
Jump to PostNext time, please copy & paste the error shown by runtime. Your assumption may be correct, but it may be incorrect under certain circumstances.
If you look at the method in Logger class, there is a variable this.input inside the method. The variable input and this.input are not the same. …
Jump to PostThere's no need to write your own logger. There are frameworks available for that.
If you're interested, you might want to take a look at these links:
Jump to PostThat looks different from what you posted earlier.
Anyway, passing null value, but later concatenating it with String won't cause the NPE. You still have not posted the exact exception/error you got from running your program... What exactly error it shows?
If you have already declared the variable, then what …
Jump to PostNope, I'm talking about parser.getCommand() which may return null or something is wrong in there. The NPE could actually comes from using null to call getCommandWord(). That's my suspicious.
By the way, the exact error should also tells you which line the error occurs. If you are not sure, add …
All 17 Replies
bguild
163
Posting Whiz
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mvmalderen
2,072
Postaholic
mumaga
0
Light Poster
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
mumaga
0
Light Poster
Taywin
312
Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.