Hello. Again I'm getting NullPointerException error. I can not figure out why.
The Activity is as follows:
Create a Java application to control the weekly menu of patients in a Nutrition Clinic. You should consider creating two classes, being:
Meal Class: contains the description of the meal, as well as information on caloric value, approximate cost and type of meal. The type of meal is a classification established by the clinic, as follows:
Diet;
Balanced;
Light;
Low carb;
Low fat;
Natural;
Gluten-free;
Vegan;
Vegetarian;
Class Menu: which should contain the weekly menu, with 6 meals daily.
Create a Test class (with the main () method) to generate a weekly menu that calculates the daily and weekly cost and calorie value of the menu.
Important notes:
To implement the type of meals, consider creating a static array in the Meal class with the values described above. In the Menu class, implement the two-dimensional array manipulation operations (include, change, traverse, and so on). Exceptionally in this project, the weekly menu display method of the Menu class may display messages in the console. Avoid doing this in other methods and in other classes, except in the Test class.
Class RestaurantTest:
public class RestauranteTeste {
public static void main(String[] args){
Cardapio semana = new Cardapio();
semana.incluirRefeicao();
}//end main method
}//end class RestauranteTeste
Reaction class:
package projeto_4;
public class Refeicao {
//Atributos da Classe
private String descricao;
private int caloria;
private double custo;
private int tipoDeRefeicao;
//atributo estatico inicializado com os valores pedidos
public static String[] classificacao = {"Diet", "Equilibrada", "Light", "Low carb", "Low fat", "Natural", "Sem glúten", "Vegano", "Vegetariano"};
/**** Metodos set e get ****/
//descricao
public void setDescricao(String descricao){
this.descricao = descricao;
}//end method setDescricao
public String getDescricao(){
return this.descricao;
}//end method getDescricao
//caloria
public void setCaloria(int caloria){
this.caloria = caloria;
}//end method setCaloria
public int getCaloria(){
return this.caloria;
}//end method getCaloria
//custo
public void setCusto(double custo){
this.custo = custo;
}//end method setCaloria
public double getCusto(){
return this.custo;
}//end method getCaloria
//tipoDeRefeicao
public void setTipoDeRefeicao(int tipoDeRefeicao){
this.tipoDeRefeicao = tipoDeRefeicao;
}//end method setTipoDeRefeicao
public String getTipoDeRefeicao(){
return classificacao[(tipoDeRefeicao-1)];
}//end method getTipoDeRefeicao
}
class Cardapio:
package projeto_4;
import java.util.Scanner;
public class Cardapio {
private Refeicao[][] menu;
public Cardapio(){
this.menu = new Refeicao[6][7];
}//end contructor
public void incluirRefeicao(){
Scanner input = new Scanner(System.in);
int i, j;
for(i = 0; i < 6 ; i++)
{
for(j = 0; j < 7; j++)
{
System.out.printf("Digite a descrição da refeição: ");
String strReceber = input.nextLine();
menu[i][j].setDescricao(strReceber);
System.out.printf("Digite a caloria: ");
int intReceber = input.nextInt();
menu[i][j].setCaloria(intReceber);
System.out.printf("Digite o Custo: ");
double floatReceber = input.nextDouble();
menu[i][j].setCusto(floatReceber);
System.out.printf("Digite o Tipo de refeição: ");
intReceber = input.nextInt();
menu[i][j].setTipoDeRefeicao(intReceber);
}//end for(j = 0; j < 7; j++)
}//end for(i = 0; i < 6 ; i++)
}//end method incluirRefeicao
public void listarMenu(){
Scanner input = new Scanner(System.in);
int i, j;
for(i = 0; i < 6 ; i++)
{
for(j = 0; j < 7; j++)
{
System.out.printf("Digite a descrição da refeição: %s",
menu[i][j].getDescricao() );
System.out.printf("Digite a caloria: %d",
menu[i][j].getCaloria() );
System.out.printf("Digite o Custo: %.02f",
menu[i][j].getCusto() );
System.out.printf("Digite o Tipo de refeição: %s",
menu[i][j].getTipoDeRefeicao() );
}//end for(j = 0; j < 7; j++)
}//end for(i = 0; i < 6 ; i++)
}//end method listarMenu
//linha][coluna
public void alteraRefeicao(int linha, int coluna){
Scanner input = new Scanner(System.in);
System.out.printf("Digite a descrição da refeição: ");
String strReceber = input.nextLine();
menu[linha][coluna].setDescricao(strReceber);
System.out.printf("Digite a caloria: ");
int intReceber = input.nextInt();
menu[linha][coluna].setCaloria(intReceber);
System.out.printf("Digite o Custo: ");
double floatReceber = input.nextDouble();
menu[linha][coluna].setCusto(floatReceber);
System.out.printf("Digite o Tipo de refeição: ");
intReceber = input.nextInt();
menu[linha][coluna].setTipoDeRefeicao(intReceber);
}//end alteraRefeicao
}//end class Cardapio
Error:
Exception in thread "main" java.lang.NullPointerException at project_4.Cardapio.incluirRefeicao (Cardapio.java:28) at proyecto_4.RestauranteTeste.main (RestauranteTeste.java:11) /home/aluno/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1 CONSTRUCTION FAIL (total time: 4 seconds)