Good morning, today I am trying to make a program which consists in entering the name of the participants of an athletics race, entering their departure time and their arrival time.
Previously in class we had made a program that has the Time Class, which the teacher told us we should do it to be able to add time, and he told us to use the constructor of that class to be able to do the program. I am currently working with 3 classes, which are:
Runners
public class Corredores {
private String nombre;
public Hora salida, llegada;
public Corredores(String nombre) {
this.nombre = nombre;
}
public String toString() {
return "La persona: " +nombre +" salio a las " +salida +" y llego a las " +llegada +
" en su prueba de atletismo";
}
}
Athletics
import java.util.Scanner;
public class Atletismo {
private static Corredores corredor [] = new Corredores[4];
private static Scanner entrada = new Scanner(System.in);
public static void main( String [] args) {
capturaDatos();
impresion();
}
public static void capturaDatos() {
String nombre;
int hora;
int minuto;
int segundo;
for(int i=0; i<corredor.length; i++) {
System.out.println("Introduzca el nombre del competidor");
nombre = entrada.nextLine();
System.out.println("Introduzca la hora de salida");
hora = entrada.nextInt();
System.out.println("Introduzca los minutos de salida");
minuto = entrada.nextInt();
System.out.println("Introduzca los segundos de salida");
segundo = entrada.nextInt();
corredor[i].salida = new Hora(hora,minuto,segundo);
entrada.nextLine(); // Buffer
}
}
public static void impresion() {
for(int i=0; i<corredor.length; i++) {
System.out.println(corredor[i].salida);
}
}
}
Time
public class Hora {
private int hr;
private int min;
private int seg;
public Hora(int hr, int min, int seg) {
this.hr = hr;
this.min = min;
this.seg = seg;
}
public void setHr(int hr) {
this.hr = hr;
}
public int getHr() {
return this.hr;
}
public void setMin(int min) {
this.min = min;
}
public int getMin() {
return this.min;
}
public void setSeg(int seg) {
this.seg = seg;
}
public int getSeg() {
return this.seg;
}
public void incrementarH(int hr) {
this.hr =this.hr + hr;
if(this.hr<=24) {
this.hr = this.hr;
}else if(this.hr>24) {
do {
this.hr = this.hr - 24;
}while(this.hr>24);
}
}
public void incrementarM(int min) {
int contador=0;
this.min = this.min+min;
if(this.min<60) {
this.min = this.min;
}else
if(this.min >=60) {
do {
this.min = this.min - 60;
contador++;
}while(this.min>=60);
hr = hr + contador;
if(hr>24) {
do {
hr = hr - 24;
}while(hr>24);
}
}
}
public void incrementarS(int seg) {
int incrementarM=0, incrementarH=0;
this.seg = this.seg+seg;
if(this.seg<60) {
this.seg = this.seg;
}else if(this.seg>= 60 && this.seg<3600) {
do {
this.seg = this.seg-60;
incrementarM++;
}while(this.seg>=60 && this.seg<3600);
min = min+incrementarM;
if(min>=60) {
do {
min = min-60;
}while(min>=60);
}
}else if(this.seg>3600) {
while(this.seg>=3600) {
this.seg = this.seg-3600;
incrementarH++;
if(this.seg>60) {
do {
this.seg = this.seg-60;
incrementarM++;
}while(this.seg>=60);
}
}
hr = incrementarH + hr;
min = min+incrementarM;
if(hr>24) {
do {
hr = hr-24;
}while(hr>24);
}
if(min>=60) {
do {
min = min-60;
}while(min>=60);
}
}
}
public String toString() {
return hr +":" +min +":" +seg;
}
}
In summary, my mistake is:
Exception in thread "main" java.lang.NullPointerException at Athletics.captureDates (Athletics.java:28) at Athletics.main (Athletics.java:6)
and I do not know why it comes out, try to do it again but omitting everything except the constructor of the Time class and it's the same.