0
Hello. Once again I’m having Nullpointerexception error. I can’t identify why.
The activity is as follows :
Create a Java app to control the weekly menu of Nutrition Clinic patients. You should consider creating two classes, being:
Class Meal: contains the description of the meal, as well as the information of caloric value, approximate cost and type of meal. The type of meal is a classification established by the clinic, according to:
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 remarks:
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 two-dimensional array manipulation operations (include, change, traverse, and etc). Exceptionally in this project, the weekly menu display method of the Menu class may display messages on the console. Avoid doing this in other methods and other classes, except in the Test class.
Restaurant class:
public class RestauranteTeste {
public static void main(String[] args){
Cardapio semana = new Cardapio();
semana.incluirRefeicao();
}//end main method
}//end class RestauranteTeste
Class Refeicao:
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.Cardapy.includedRejection(Cardapio.java:28) at project_4.RestauranteTeste.main(Restauranttest.java:11) /home/student/. cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1 CONSTRUCTION FAILURE (total time: 4 seconds)
Hello. I implemented with the idea of @Sósthenes Neto. But thank you anyway. You can’t mark your answer as relevant because I don’t have enough reputation to do it yet :( Then I’ll try to use 'if (Objects.isNull(menu[i][j])'.
– Thiago Soares Mota
@Thiagosoaresmota You can vote +1 for all answers you find useful.
– Piovezan