I've been working on this code for a while now, and right after I finally get it to compile it gives me an error 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)
The purpose of the code is to convert military time to normal time and increment it according to the provided sample outputs. Here's the code:
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;
}
}
}
}
After I read the nullpointerexception quesstion, I originally thought it was an issue with an element in the array not being defined, but when I changed the for loop length to test this it game me the same error. Now, I have no idea what the issue in my code is. Thanks in advance.
The error you have posted does not correspond with the code posted.
You are not getting an NPE, and if you are , you have other code that is not present.
When I run your code, I am getting the following
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at SO42236589.main(SO42236589.java:30)
Indicating that you have an issue on line:
for ( int i = 0; i < 8; i++ )
You should replace this with something like
for ( int i = 0; i < mtimes.length - 1; i++ )
You program also probably fails to run because your main
class is not static, and nether are any of the called upon methods.