I'm getting a NullPointerException and after doing research I can't figure out why. The error message reads:
java.lang.NullPointerException
at TextDecoder.readMessagesFromFile(TextDecoder.java:32)
at TextDecoder.main(TextDecoder.java:54)
I've commented next to lines 54 and 32.
This is my constructor (all of these methods come from the same class):
public TextDecoder() {
messages = new TextMessage[10];
msgCount = 0;
}
My Main method is the following (I included class declaration):
public class TextDecoder {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("There is an incorrect amount of command-line arguments. Command-line Argument length: " + args.length);
System.exit(1);
}
File msgFile = new File(args[0]);
if (msgFile == null) {
System.out.println("File not found.");
System.exit(1);
}
readMessagesFromFile(msgFile); // Line 32
}
}
My readMessagesFromFile is the following:
public static void readMessagesFromFile(File inputMsgFile) throws Exception {
Scanner input = new Scanner(inputMsgFile);
while (input.hasNextLine()) {
/* Some code here */
// Recipient and keyPresses are defined in here and are not null
}
messages[msgCount] = new TextMessage(recipient, keyPresses); // Line 54
msgCount++;
}
input.close();