Java - NullPointerException原因

时间:2014-05-24 21:09:15

标签: java nullpointerexception

我最近开始学习Java,在我正在做的课程中我遇到以下错误:

Exception in thread "main" java.lang.NullPointerException
at Clock.<init>(Clock.java:7)
at Main.main(Main.java:3)
Java Result: 1

我大致了解null异常的错误是什么意思,但无法解决如何在我的代码中解决它:

Clock.java

public class Clock {
    private BoundedCounter hours;
    private BoundedCounter minutes;
    private BoundedCounter seconds;

    public Clock(int hoursAtBeginning, int minutesAtBeginning, int secondsAtBeginning) {       
        this.hours.setValue(hoursAtBeginning);
        this.minutes.setValue(minutesAtBeginning);
        this.seconds.setValue(secondsAtBeginning);

    }

    public void tick() {
        // Clock advances by one second
        seconds.setValue(seconds.getValue() + 1);
    }

    public String toString() {
        // returns the string representation
        return hours+":"+minutes+":"+seconds;
    }
}

Main.Java

public class Main {
public static void main(String[] args) {
    Clock clock = new Clock(23, 59, 50);

    int i = 0;
    while( i < 20) {
        System.out.println( clock );
        clock.tick();
        i++;
    }
}
}

如果有人能够指出我所缺少的东西,那么它将会非常有用!

非常感谢,

Sapples

0 个答案:

没有答案