[英]why am i getting error: “Exception in thread ”main“ java.lang.NullPointerException”
When i run my program i get the following error: 当我运行程序时,出现以下错误:
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)
despite my best efforts i cannot identify the problem i believe that it my have to do with the way i am declaring my program. 尽管我已经尽了最大的努力,但我仍然无法确定问题所在,我认为这与我声明程序的方式有关。
could please help by identifying and providing a solution to my problem this will be much appreciated. 可以通过识别并提供解决我的问题的方法来提供帮助,我们将不胜感激。
here is the problem code: Interface class: 这是问题代码:接口类:
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);
}
}
the next class: 下课:
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++;
}
and final the array class and relevant methods: 最后是数组类和相关方法:
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;
}
these pieces of code are not the complete classes but rather the methods that i fell are relevant to my question. 这些代码不是完整的类,而是我所使用的方法与我的问题有关。
You have an array diary
with null references of HomeWork
so before using it as diary[count]
you have to first initialize it as diary[count]=new HomeWork();
您有一个数组
diary
其中带有HomeWork
空引用 ,因此在将其用作diary[count]
,必须首先将其初始化为diary[count]=new HomeWork();
in your HomeWorkDiary
constructor. 在您的
HomeWorkDiary
构造函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.