2012-12-26 26 views
-2

爲什麼當我從Time2類的toString中調用Date類的toString時,出現以下錯誤消息?在Exerciseschpt8.Time2Test.main(Time2Test.java:18) 異常在線程 「主」 顯示java.lang.NullPointerException 在Exerciseschpt8.Time2.toString(Time2.java:111) 從其他類的toString調用toString方法。不重新創建值

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()); 

} }

+0

定義'd1'後,添加't1.dateX = d1;'。原因是你從未初始化'dateX'。 – Shivam

回答

0

你無處你Time2類初始化dateXTime2類的toString()方法,使用的方法就可以了(dateX.toString())。這是導致有效的NullPointerException

要解決該問題,請根據您的程序初始化dateX

0

在行Date dateX;你聲明的是dateX,但你沒有初始化它,所以它是一個空指針。要初始化它,請將行更改爲Date dateX = new Date();