NullPointerException in time-handling code

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.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:");; 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( at TimeTest.main( 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

以上就是NullPointerException in time-handling code的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准