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

The test class

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;

It is giving me the error

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

I know this is NOT this code's only problem, so if you have any advice I'm all ears.

  • Could you post line 39 of `Time.java`? – Vidya Oct 22 '13 at 23:33
  • " I know my code looks like a hot mess so bear with me....." Making the code less of a hot mess is supposed to be for *your* benefit, not ours. Proper formatting will save you a lot of headaches over the long run. – Dennis Meng Oct 22 '13 at 23:35
  • Well, @Dennis, it wasn't so hot a mess that you were unable to figure out the problem. But yes, proper formatting is really helpful. – Vidya Oct 22 '13 at 23:39

2 Answers2

1

My guess is that the error is here:

TimeofDay.equals("AM");

That just checks whether TimeofDay is AM (but you never use the return value of the function, so it doesn't do much other than throw a NullPointerException when TimeofDay is null).

Did you perhaps mean to assign the value?

TimeofDay = "AM";
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

My guess is you have declared variable in line 5

   public String valid, TimeofDay;

and you are using equals on this variable in line 39 without initializing it anywhere:

   TimeofDay.equals("AM");

You are using equals on variable which has not been initialized. That might be the reason you are getting NullPointerException.

Rajesh Golani
  • 448
  • 1
  • 6
  • 13