-1
public  class Time {

    public int Minutes, Hours, TotalMin;
    private String  correctedMinutes ;
    public String valid, TimeofDay;
    public static int error;


    //-----------------------------------------------------------------
    //  Sets up the time by defining its minutes and hours 
    //-----------------------------------------------------------------
public  Time(int  hours, int minutes)
    { if (minutes>59|| minutes<0|| hours>23 || hours<0) 
              { Hours=0;
       Minutes=0;
          error=1;}


      else
      {Minutes=minutes;
      Hours=hours;
          error=0;}

      }

public String Time2(int hours, int minutes, String timeofday) {
         if (Hours>12 || Hours<0)
        return "This is not a valid number of hours";
         Hours=0;

         if ( minutes>59|| minutes<0)
                 {Minutes=0;
                 error=1;
                return "This is not a valid number of minutes";}

         if (!timeofday.equals ("AM") || !timeofday.equals ("PM"))
         {TimeofDay.equals("AM");
         return "You incorrectly enetered AM or PM";}


              else 
              { Hours=hours;
            Minutes=minutes;
                return "";}


            }

    //----------------------------------------------------------
    //
    //
    //-----------------------------------------------------------

public int addMinutes (int hours, int minutes, int elapsedMin)
    { 
        if (elapsedMin<0)
        return error=1;

            else {
            TotalMin=elapsedMin+Minutes;
                    return error=0;}
        }{

    while (TotalMin>60)
            {TotalMin=TotalMin-60;
            Hours=Hours+1;   if (Hours==25)
             { Hours=0;}

             Minutes=TotalMin;
             }



    }
    //----------------------------------------------------------
    //
    //
    //-----------------------------------------------------------

        public String toString ()
        {
            String correctedMinutes = String.format("%02d", Minutes);
            return (Hours+ ":" + correctedMinutes + TimeofDay);     }

测试类

public class TimeTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


int minutes, hours, elapsedMin; 
String timeofday, startTime;

Scanner scan=new Scanner (System.in);

System.out.println("Please enter starting hour (1-12):");
hours=scan.nextInt();
System.out.println("Please enter starting minute (0-59):");
minutes=scan.nextInt();
System.out.println("Please enter AM or PM:");
timeofday=scan.next();


Time classtime = new Time (hours, minutes);

if (Time.error!=0)
    System.out.println("This is an invalid time");


System.out.print(classtime.Time2(hours, minutes, timeofday));

{
    if (Time.error!=0)
        System.out.println("This is an invalid time");





System.out.print("The class begins at ");
System.out.println (classtime.toString());

startTime=classtime.toString();

System.out.println("Enter a time in minutes representing the class duration:");
elapsedMin=scan.nextInt();

classtime.addMinutes (hours, minutes, elapsedMin);
System.out.println (classtime.toString());


System.out.print(elapsedMin + " minutes after "+ startTime + " is ");
System.out.println (classtime.toString());





System.out.print("The class ends at ");
System.out.println (classtime.toString());


System.out.print("The class ends at ");

System.out.print("MILITARY");
}}}
import java.util.Scanner;

它给了我错误

Exception in thread "main" java.lang.NullPointerException
at Time.Time2(Time.java:39)
at TimeTest.main(TimeTest.java:29)

我知道这不是这段代码唯一的问题,所以如果你有任何建议,我会全力以赴。

4

2 回答 2

1

我的猜测是错误在这里:

TimeofDay.equals("AM");

这只是检查是否TimeofDay是(但你从不使用函数的返回值,所以除了抛出一个when isAM之外它没有做太多事情)。NullPointerExceptionTimeofDaynull

您可能是要分配价值吗?

TimeofDay = "AM";
于 2013-10-22T23:34:06.710 回答
0

我的猜测是你已经在第 5 行声明了变量

   public String valid, TimeofDay;

并且您在第 39 行对这个变量使用 equals 而不在任何地方对其进行初始化:

   TimeofDay.equals("AM");

您在尚未初始化的变量上使用 equals。这可能是您收到 NullPointerException 的原因。

于 2013-10-22T23:48:11.277 回答