当我运行我的程序时,我收到以下错误:
Exception in thread "main" java.lang.NullPointerException
at study.calendar.HomeWorkDiary.<init>(HomeWorkDiary.java:51)
at study.calendar.HomeworkTeftInterFace.main(HomeworkTeftInterFace.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
尽管我付出了最大的努力,但我无法确定问题,我认为这与我宣布我的计划的方式有关。
可以通过识别并提供我的问题的解决方案来提供帮助,我们将非常感激。
这是问题代码: 接口类:
public class HomeworkTeftInterFace {
public static void main(String[] args) {
HomeWorkDiary tefo = new HomeWorkDiary("test");
String task1 = JOptionPane.showInputDialog("task");
String subject1 = JOptionPane.showInputDialog("subject");
double priorty1 = (Double.parseDouble(JOptionPane.showInputDialog("priorty")));
int day1 = parseInt(JOptionPane.showInputDialog("due day"));
int month1 = parseInt(JOptionPane.showInputDialog("month"));
int year1 = parseInt(JOptionPane.showInputDialog("year"));
tefo.addTask(task1, subject1, priorty1, day1, month1, year1);
}
}
下一堂课:
public HomeWorkDiary(String filename) {
// System.out.println("test5");
diary = new Homework[count + 50];
count = 0;
// System.out.println("test6");
try {
Scanner file = new Scanner(new File("HomeWork.txt"));
file.useDelimiter("#");
while (file.hasNextLine()) {
// System.out.println("test3");
String task = file.next();
String subject = file.next();
// System.out.println("test7");
String priorty = file.next();
// System.out.println("test9");
String day = file.next();
String month = file.next();
String year = file.next();
// System.out.println("test");
diary[count].setTaks(task);
diary[count].setSubject(subject);
diary[count].setPrioity(Double.parseDouble(priorty));
diary[count].setDay(parseInt(day));
diary[count].setmonth(parseInt(month));
diary[count].setyear(parseInt(year));
// System.out.println("test 10");
count++;
file.nextLine();
}
} catch (FileNotFoundException f) {
}
System.out.println("test4");
}
public void addTask(String task1, String subject1, double priorty1, int day1, int month1, int year1) {
diary[count].setTaks(task1);
diary[count].setSubject(subject1);
diary[count].setPrioity(priorty1);
diary[count].setDay(day1);
diary[count].setmonth(month1);
diary[count].setyear(year1);
count++;
}
并最终得到数组类和相关方法:
public class Homework {
private String subject;
private String task;
private double priorty;
private int year;
private int month;
private int day;
Homework(String task, String subject, double priorty, int year, int month, int day) {
this.task = task;
this.subject = subject;
this.priorty = priorty;
this.year = year;
this.month = month;
this.day = day;
}
public void setTaks(String task1) {
System.out.println(task1);
task = task1;
}
public void setSubject(String subject1) {
System.out.println("test");
subject = subject1;
}
public void setPrioity(double priorty1) {
priorty = priorty1;
}
public void setDay(int day1) {
day = day1;
}
public void setyear(int year1) {
year = year1;
}
public void setmonth(int month1) {
month = month1;
}
这些代码不是完整的类,而是我所涉及的方法与我的问题相关。
答案 0 :(得分:0)
您有diary
的数组HomeWork
,diary[count]
的空引用,因此在将其用作diary[count]=new HomeWork();
之前,您必须先将其初始化为HomeWorkDiary
在{{1}}构造函数中。