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

これは is かどうかをチェックするだけTimeofDayです(ただし、関数の戻り値を使用することはないため、 when isAMをスローする以外には何もしません)。NullPointerExceptionTimeofDaynull

おそらく値を割り当てるつもりでしたか?

TimeofDay = "AM";
于 2013-10-22T23:34:06.710 に答える