当我运行程序时,出现以下错误:

 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;
    }


这些代码不是完整的类,而是我所使用的方法与我的问题有关。

最佳答案

您有一个diary数组,该数组具有HomeWork的空引用,因此在将其用作diary[count]之前,必须首先在diary[count]=new HomeWork();构造函数中将其初始化为HomeWorkDiary

10-04 20:22