-2

当我从 Time2 类的 toString 调用 Date 类的 toString 时,为什么会收到以下错误消息?Exerciseschpt8.Time2.toString(Time2.java:111) 处的线程“main”java.lang.NullPointerException 中的异常在 Exerciseschpt8.Time2Test.main(Time2Test.java:18)

package Exerciseschpt8;



public class Date{
    private int month; // 1-12
    private int day; // 1-31 based on month
    private int year; // any year

    public Date(){

        this(0,0,0);
    }

    public Date(int theMonth, int theDay, int theYear) {
        month = checkMonth(theMonth); // validate month
        year = theYear; // could validate year
        day = checkDay(theDay); // validate day

        System.out.printf("Date object constructor for date %s\n", this);
    }

    private int checkMonth(int month) {
        if (month > 0 && month <= 12) // validate month
            return month;
        else // month is invalid
        {
            System.out.printf("Invalid month (%d) set to 1.", month);
            return 1; // maintain object in consistent state
        } // end else
    } // end method checkMonth

    // utility method to confirm proper day value based on month and year
    private int checkDay(int day) {
        int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 28, 31, 30, 31, 30,
                31 };

        // check if day in range for month
        if (day > 0 && day <= daysPerMonth[month])
            return day;

        // check for leap year
        if (month == 2 && day == 29
                && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
            return day;

        System.out.printf("Invalid day (%d) set to 1.", day);
        return 1; // maintain object in consistent state
    } // end method checkDay

    // return a String of the form month/day/year
    public String toString() {
        return  ""+month+"/"+ day+"/"+year;
    } // end method toString

    public int nextDay() {
        int testDay = day + 1;

        if (checkDay(testDay) == testDay)
            day = testDay;
        else {
            day = 1;
            //nextMonth();
        }
        return day;

    }

    public int nextMonth() {
        if (1 == month)
            month++;
        return month = 1;

    }

    public String toDateString() {
        return month + "/" + day + "*/" + year;
    }

} 


package Exerciseschpt8;

import Exerciseschpt8.Date;

public class Time2 {
    Date dateX;
    private int hour; // 0 - 23
    private int minute; // 0 - 59
    private int second; // 0 - 59

    public Time2() {
        this(0, 0, 0);
    }

    public Time2(int h) {
        this(h, 0, 0);
    }

    public Time2(int h, int m) {
        this(h, m, 0);
    }

    public Time2(int h, int m, int s) {
        setTime(h, m, s);
    }

    public Time2(Time2 time) {

        this(time.getHour(), time.getMinute(), time.getSecond());
    }

    public boolean setTime(int h, int m, int s) {

        boolean hourValid, minuteValid, secondValid;
        hourValid = setHour(h); // set the hour
        minuteValid = setMinute(m); // set the minute
        secondValid = setSecond(s); // set the second

        return (hourValid && minuteValid && secondValid);
    }

    public boolean setHour(int h) {
        // hour = ((h >= 0 && h < 24) ? h : 0);
        if (h >= 0 && h < 24) {
            hour = h;
            return true;
        } else {
            hour = 0;
            return false;
        }
    } // end method setHour

    public boolean setMinute(int m) {
        // minute = ((m >= 0 && m < 60) ? m : 0);
        if (m >= 0 && m < 60) {
            minute = m;
            return true;
        } else {
            minute = 0;
            return false;
        }

    } // end method setMinute

    public boolean setSecond(int s) {
        // second = ((s >= 0 && s < 60) ? s : 0);
        if (s >= 0 && s < 60) {
            second = s;
            return true;
        } else {
            second = 0;
            return false;
        }
    } // end method setSecond

    public int getHour() {
        return hour;
    } // end method getHour

    public int getMinute() {
        return minute;
    } // end method getMinute

    public int getSecond() {
        return second;
    } // end method getSecond

    // Tick the time by one second
    public void tick() {
        setSecond(second + 1);

        if (second == 23)
            incrementMinute();
    }

    public void incrementMinute() {
        setMinute(minute + 1);

        if (minute == 25)
            incrementHour();
    }

    public void incrementHour() {
        setHour(hour + 1);

        if (hour == 0)
            dateX.nextDay();
    }

    public String toString() {
        return + hour + ":" + minute + ":" + second + "\n"+dateX.toString();
    }

package Exerciseschpt8;

public class Time2Test {
    public static void main(String[] args) {
        Time2 t1 = new Time2(2,15,23); // 00:00:00
/
        Date d1 = new Date(10,23,1973);
        //System.out.println(d1.toDateString());

        System.out.println("Constructed with:");
        System.out.println("t1: all arguments defaulted");
        //System.out.printf(" %s\n", t1.toUniversalString());
        System.out.printf(" %s\n", t1.toString());

} }

4

2 回答 2

0

您无法在类中初始化dateX并在类的方法中使用其上的方法Time2dateX.toString())。这导致了有效的.toString()Time2NullPointerException

dateX要解决此问题,请根据您的程序进行适当的初始化。

于 2012-12-26T02:49:41.273 回答
0

Date dateX;您声明 dateX 的行中,但您没有对其进行初始化,因此它是一个空指针。要初始化它,请将行更改为Date dateX = new Date();

于 2012-12-26T02:50:03.793 回答