I have to make a program that introduced Monday, February, 2019, transforms that data to, for example, 02/05/2019. Where 05 has to correspond to the first Monday of the month of February 2019.
For now I am trying to make a class that contains the data of the DayOfWeek and Month enums in the default language of the client computer, in this case, Spanish.
Then, I want to make another class, which calls a single instance of the other to validate that the day that they have entered corresponds to one of the days in the source language, since I am speaking Spanish, I should enter Monday and this should return that it works.
I show you the two codes:
package exercise8;
import java.time.DayOfWeek;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.Locale;
public class SpanishsInfo {
/* Atributos */
/* Array que almacena los meses en español.*/
private String[] months;
/* Array que almacena los días en español.*/
private String[] days;
/* Constructor */
public SpanishsInfo() {
setArrays();
}
/* Getters */
public String[] getMonths() {
return months;
}
public String[] getDays() {
return days;
}
/* Inicializador de array para meses */
private void setArrays() {
byte i = 0;
/* Obtenemos la palabra para el idioma en el que se ejecuta el programa. */
Locale l = new Locale(Locale.getDefault().toLanguageTag());
/* Recorre el enum de los meses y los agrega al array month*/
for(Month m : Month.values()) {
months[i] = m.getDisplayName(TextStyle.FULL, l);
i++;
}
i = 0;
/* Recorre el enum de los días y los agrega al array days*/
for(DayOfWeek d : DayOfWeek.values()) {
days[i] = d.getDisplayName(TextStyle.FULL, l);
i++;
}
}
}
package exercise8;
import java.util.Arrays;
import java.util.Scanner;
import exercise8.ConsoleInput;
public class CheckWord {
/* Atributos */
/* Instancia un objeto de SpanishInfo para obtener los días y meses correctos. */
private static SpanishsInfo info = new SpanishsInfo();
/* Instancias para pedir por teclado. */
private static Scanner keyboard = new Scanner(System.in);
private static ConsoleInput kb = new ConsoleInput(keyboard);
/* Atributo global para devolver los días y meses. */
private static String result = null;
/* Constructor */
private CheckWord() {};
/* Inicio */
public static String getDay() {
do {
System.out.println("Introduzca un día de la semana: ");
/* Pide un día por teclado */
result = kb.readString().toLowerCase();
/* Comprueba que la palabra introducida esté dentro
* del array pasado. En caso contrario, vuelve a pedir el día. */
} while (Arrays.binarySearch(info.getDays(), result) > 0);
/* Si el día se encuentra dentro del array, se devuelve la palabra. */
return result;
}
}
I have an App class with a main, in which all I do is
String day = getDay();
A priori everything is imported. When I start the program, I get the following problem:
Exception in thread "main" java.lang.ExceptionInInitializerError
at exercise8.App.main(App.java:11)
Caused by: java.lang.NullPointerException
at exercise8.SpanishsInfo.setArrays(SpanishsInfo.java:43)
at exercise8.SpanishsInfo.<init>(SpanishsInfo.java:21)
at exercise8.CheckWord.<clinit>(CheckWord.java:12)
... 1 more
I'm trying to make everything static, because that seems to me to be the best way to do it.
Can someone tell me what I'm doing wrong?
Thank you very much.
The error is giving you in this line:
because the array
months
is not initialized at any time. So itmonths
's null, and it throws you an exception because a null array position can't be accessed.You'll have to do something like this at some point:
(where MAX_MONTHS would be the maximum size of the array, I guess it will be 12).
More info: