Я получаю исключение NullPointerException и после исследования не могу понять, почему.Сообщение об ошибке гласит:
java.lang.NullPointerException
at TextDecoder.readMessagesFromFile(TextDecoder.java:32)
at TextDecoder.main(TextDecoder.java:54)
Я прокомментировал рядом со строками 54 и 32.
Это мой конструктор (все эти методы принадлежат одному и тому же классу):
public TextDecoder() {
messages = new TextMessage[10];
msgCount = 0;
}
Мой метод Main следующий (я включил объявление класса):
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
}
}
Мой readMessagesFromFile следующий:
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();