Java Edhesive代码中的NullPointerException
我已经为这段代码工作了一段时间,在我最终编译它之后,它给了我一个错误代码:Java Edhesive代码中的NullPointerException,java,drjava,Java,Drjava,我已经为这段代码工作了一段时间,在我最终编译它之后,它给了我一个错误代码: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.Delegating
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)
代码的目的是将军事时间转换为正常时间,并根据提供的样本输出增加时间。代码如下:
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循环长度来测试这个问题时,我遇到了相同的错误。现在,我不知道代码中的问题是什么。提前感谢。您发布的错误与发布的代码不一致
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)
你没有得到NPE,如果你得到了,你还有其他不存在的代码
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)
当我运行你的代码时,我得到以下信息
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)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at SO42236589.main(SO42236589.java:30)
表示您在线上有问题:
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)
for ( int i = 0; i < 8; i++ )
你应该用类似的东西来代替它
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)
for ( int i = 0; i < mtimes.length - 1; i++ )
您的程序也可能无法运行,因为您的主类不是静态的,下面是调用的任何方法。非常感谢。考虑到我基本上是在尝试让任何慷慨的灵魂校对我的评论,而不是混乱的代码,你仍然花时间阅读并回应。我对你感激不尽。
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)