Edhesive代码

时间:2017-02-14 21:29:54

标签: java drjava

我已经在这段代码上工作了一段时间,在我最终编译之后,它给了我一个错误代码:

java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

代码的目的是将军事时间转换为正常时间,并根据提供的样本输出递增。这是代码:

import java.io.*;
import java.util.Scanner;
public class Time2{
public void main(String args []) {
   String smtimes1 = "blank";
   String smtimes2 = "blank";
   String smtimes3 = "blank";
   String smtimes4 = "blank";
   String smtimes5 = "blank";
   String smtimes6 = "blank";
   String smtimes7 = "blank";

   String[] smtimes = { smtimes1, smtimes2, smtimes3, smtimes4, smtimes5, smtimes6, smtimes7 };
   int[] mtimes = new int [7];
   int[] times = new int [7];
   int[] h = new int [7];
   int[] m = new int [7];
   int[] hi = new int [7];
   int[] mi = new int [7];
   String[] ap = new String [7];

   Scanner scan = new Scanner(System.in);
   System.out.println("Please enter your times:");

   for(int i = 0; i < 8; i++){
     mtimes[i] = scan.nextInt();
   }

   for(int i = 0;i < 8;i++){
     h[i] = (mtimes[i]/100);
     m[i] = (mtimes[i] - (h[i] * 100));

    System.out.println( h[i] + " " + i + "  " + m[i] + " " + i);  
   }
   ////////////////////////
   time(h , m);

   increment(h, m, hi, mi);

   convert(h , m , ap);

   toString(mtimes, smtimes1, smtimes2, smtimes3, smtimes4, smtimes5, smtimes6, smtimes7);
   ///////////////////////
   for(int i = 0;i < 7;i++){
     System.out.println( "time" + (i+1) + ": " + smtimes[i]); 
     System.out.println( "convert time" + (i+1) + " to standard time: " + h[i] + ":" + m[i] + " " + ap[i]);

     if(i == 0){
       System.out.println( "time" + (i+1) + ": " + smtimes[i]);
       System.out.println( "increment time" + (i+1) + " five times: " + (h[i] + 1) + "01" );
     }
     if(i == 1){
       System.out.println( "time" + (i+1) + ": " + smtimes[i]);
       System.out.println( "increment time" + (i+1) + " five times: " + "0" + (h[i] + 1) + "19" );
     }
     if(i == 6){
       System.out.println( "time" + (i+1) + ": " + smtimes[i]);
       System.out.println( "increment time" + (i+1) + ": " + "0" + (h[i] + 1) + "19");
       System.out.println( "time" + (i+1) + ": " + "0000");
       System.out.println( "time" + (i+1) + ": " + (i+6) + ":" + "00 AM");
     }

   }




}


 /*
  * If h is between 1 and 23 inclusive, set the hour to h. 
  * Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, 
  * set the minutes to m. Otherwise, set the minutes to 0. 
  */ 
 public static int time(int h[], int m[]) {
  for(int i = 0;i < 7;i++){
   if( ( h[i] > 1 ) && ( h[i] < 23 ) ){
     h = h;
   }
   else{
     h[i] = 0;
   }
   if( ( m[i] > 0 ) && ( m[i] < 59 ) ){
     m[i] = m[i];   
   }
      else{ 
        m[i] = 0;
      }
  }
   return 0;                
 }

 /* Returns the time as a String of length 4 in the format: 0819. 
  * Notice that if the hour or minute is one digit, it should 
  * print a zero first. For example, 6 should print as 06.
 */
 public String toString(int mtimes[], String smtimes1, String smtimes2, String smtimes3, String smtimes4, String smtimes5, String smtimes6, String smtimes7) {
     //
     if( (Integer.toString(mtimes[0]).length()) < 4){
       smtimes1 = ("0" + Integer.toString(mtimes[0]));
     }
     else{
       smtimes1 = (mtimes[0] + " ");
     }
     //
     if( (Integer.toString(mtimes[1]).length()) < 4){
       smtimes2 = ("0" + mtimes[1]);
     }
     else{
       smtimes2 = (mtimes[1] + " ");
     }
     //
     if( (Integer.toString(mtimes[2]).length()) < 4){
       smtimes3 = ("0" + mtimes[2]);
     }
     else{
       smtimes3 = (mtimes[2] + " ");
     }
     //
     if( (Integer.toString(mtimes[3]).length()) < 4){
       smtimes4 = ("0" + mtimes[3]);
     }
     else{
       smtimes4 = (mtimes[3] + " ");
     }
     //
     if( (Integer.toString(mtimes[4]).length()) < 4){
       smtimes5 = ("0" + mtimes[4]);
     }
     else{
       smtimes5 = (mtimes[4] + " ");
     }
     //
     if( (Integer.toString(mtimes[5]).length()) < 4){
       smtimes6 = ("0" + mtimes[5]);
     }
     else{
       smtimes6 = (mtimes[5] + " ");
     }
     //
     if( (Integer.toString(mtimes[6]).length()) < 4){
       smtimes7 = ("0" + mtimes[6]);
     }
     else{
       smtimes7 = (mtimes[6] + " ");
     }
     //  

   return "";
 }


 public String convert(int h[], int m[], String ap[]) {

   for(int i = 0;i < 7;i++){
     if(h[i] > 12){ //13 --> 1
       h[i] = (h[i] - 12);
       ap[i] = "PM"; 
     }
     else{
       ap[i] = "AM"; 
     }

   }

   return "";
 }

/*
 * Advances the time by one minute. 
 * Remember that 60 minutes = 1 hour. 
 * Therefore, if your time was 0359, and you add one minute, 
 * it becomes 0400. 2359 should increment to 0000.
 */ 
public void increment(int h[],int m[], int hi[], int mi[]) {

  for(int i = 0; i< 7; i++){
    mi[i] = m[i] + 1;
    if( mi[i] == 60){
      hi[i] = h[i] + 1;
      mi[i] = 00;
    }

  }

}  
}

在我阅读nullpointerexception quesstion之后,我原本以为这是一个问题,数组中的元素没有被定义,但是当我更改for循环长度来测试它时,它给我带来了同样的错误。现在,我不知道我的代码中的问题是什么。提前谢谢。

2 个答案:

答案 0 :(得分:0)

计算零

如果用7位置初始化数组,那么你应该循环到7而不是8,所以t[0], t[1], t[2], t[3], t[4], t[5], t[6]这7,但在你的代码中你用7启动数组并用8循环:

int[] mtimes = new int [7];

for(int i = 0; i < 8; i++){
   mtimes[i] = scan.nextInt();
}

将循环更改为:

for(int i = 0; i < 7; i++){
    mtimes[i] = scan.nextInt();
}

或者增加数组的大小:

int[] mtimes = new int [8];

答案 1 :(得分:0)

您发布的错误与发布的代码不符。

您没有获得NPE,如果您是,您还有其他不存在的代码。

当我运行你的代码时,我得到以下

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at SO42236589.main(SO42236589.java:30)

表示您有问题在线:

for ( int i = 0; i < 8; i++ )

你应该用

之类的东西替换它
for ( int i = 0; i < mtimes.length - 1; i++ )

你的程序也可能无法运行,因为你的main类不是静态的,而nether是任何被调用的方法。