粘性代码中的 NullPointerException
NullPointerException in Edhesive code
我已经研究这段代码一段时间了,在我终于编译它之后它给了我一个错误代码:
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的问题后,我本来以为是数组中某个元素没有被定义的问题,但是当我改变for循环长度来测试它时,它让我遇到了同样的错误。现在,我不知道我的代码中的问题是什么。提前致谢。
您发布的错误与发布的代码不符。
您没有获得 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
class 不是静态的,调用的方法也不是静态的。
我已经研究这段代码一段时间了,在我终于编译它之后它给了我一个错误代码:
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的问题后,我本来以为是数组中某个元素没有被定义的问题,但是当我改变for循环长度来测试它时,它让我遇到了同样的错误。现在,我不知道我的代码中的问题是什么。提前致谢。
您发布的错误与发布的代码不符。
您没有获得 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
class 不是静态的,调用的方法也不是静态的。