1
How do I "join" or concatenate an object within a collection of different objects? I’ve tried redoing but it always gives the same error.
public class Prova {
private String titulo;
private int numQuestoes;
private String disciplina;
private double notaMaxima;
private String professor;
private int id = 0;
Questao[] questao = new Questao[100];
static int contador = 0;
Prova(String titulo, int numQuestoes , String disciplina , double notaMaxima , String professor){
contador++;
this.id = Prova.contador;
this.titulo = titulo;
this.numQuestoes = numQuestoes;
this.disciplina = disciplina;
this.notaMaxima = notaMaxima;
this.professor = professor;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo){
this.titulo = titulo;
}
public int getNumQuestoes() {
return numQuestoes;
}
public void setNumQuestoes(int numQuestoes) {
this.numQuestoes = numQuestoes;
}
public String getDisciplina() {
return disciplina;
}
public double getNotaMaxima() {
return notaMaxima;
}
public String getProfessor() {
return professor;
}
public int getId() {
return id;
}
public void mostrarProva() {
System.out.println("ID: "+id);
System.out.println("Título: "+titulo);
System.out.println("Número de Questões: "+numQuestoes);
System.out.println("Disciplina: "+disciplina);
System.out.println("Nota Máxima: "+notaMaxima);
System.out.println("Professor: "+professor);
//System.out.println("\n");
}
public class Questao {
private int numero;
//private String item;
private String enunciado;
private String resposta;
static int contador = 0;
private int id;
Questao(int numero, String enunciado, String resposta){
contador++;
this.id = Questao.contador;
this.numero = numero;
this.enunciado = enunciado;
this.resposta = resposta;
}
public int getNumero(){
return numero;
}
public String getEnunciado(){
return enunciado;
}
public String getResposta() {
return resposta;
}
public int getId() {
return id;
}
public void mostrarQ() {
System.out.println("ID :"+id);
System.out.println("Numero : "+numero);
System.out.println("Enunciado : "+enunciado);
System.out.println("Resposta : "+resposta);
}
}
public class CadastroDeProvas {
Prova[] provas = new Prova[100];
int qtdProvas = 0;
int qtdQuest = 0;
void adicionarQuestao (int i, Questao questao){
provas[i].questao[qtdQuest] = questao;
qtdQuest++;
}
void imprimir(){
for(int i = 0; i < qtdProvas; i++){
provas[i].mostrarProva();
for(int j=0;j < qtdQuest;j++) {
provas[i].questao[j].mostrarQ();
}
}
}
}
The parameter int i
is the position of the object of the vector of provas[]
that concatenated with the vector questao[]
will receive the object questao
.
void imprimir(){
for(int i = 0; i < qtdProvas; i++){
provas[i].mostrarProva();
for(int j=0;j < qtdQuest;j++) {
provas[i].questao[j].mostrarQ();
}
}
}
Every time I run the code imprimir()
it gives error in this method, more specifically in the line highlighted below:
provas[i].questao[j].mostrarQ();
public class Questao {
private int numero;
//private String item;
private String enunciado;
private String resposta;
static int contador = 0;
private int id;
The mistake more specifically:
Exception in thread "main" java.lang.NullPointerException at br.ufc.crateus.provas.CadastroDeProvas.imprimir(CadastroDeProvas.java:26) at br.ufc.crateus.provas.Principal.main(Principal.java:92)
What are you trying to do? What goal do you want to achieve? The question got me a little confused and I think you may need to edit your question. Try giving a space between classes to visibly group them. Where is the Questao class?
– Carlos Cariello
The program so far is making a record of evidence and question. And what I want to do is to add a question to a certain piece of evidence, that is, when I print the data of a piece of evidence, the data of the respective question can be shown tbm.
– Ícaro Timóteo
For example: I register a proof "P1" and a question "Q1". I wanted the "Q1" to be concatenated with "P1", I don’t know if "concatenate" is the best word, but that’s more or less what I tried to do in the code above. However, if I register more than ONE proof and try to concatenate a question, the program gives error when printing().
– Ícaro Timóteo
What do you mean by concatenate proof and question ? Concatenating in programming means "joining texts", not objects.
– Isac
That’s why I put it in quotes. It was just for lack of a better word.
– Ícaro Timóteo
What I’m really wanting to do is in the above comments.
– Ícaro Timóteo