0

I'm writing a program that could manage universitary students with courses and subjects. The problem of the class I'm showing you is that when the method

public CorsoStudi(String unNomeCorso, ArrayList unElencoMaterie, int unIdCorso)

is called it throws a NullPointerException at line elencoEsamiDati.add(q);. So, probably the problem is with this line and the line after this:

EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);

Eclipse doesn't advice error on writing code.

Creatore.java

import java.util.ArrayList;
import java.util.Scanner;

public class Creatore {

int counterStudenti = 0;
int counterMaterie = 0;
int counterCorsi = 0;
//int counterEsami = 0;
ArrayList<Studente> listaStudenti = new ArrayList<Studente>();
ArrayList<Materia> listaMaterie = new ArrayList<Materia>();
ArrayList<CorsoStudi> listaCorsi = new ArrayList<CorsoStudi>();
//ArrayList<EsameDato> listaEsami = new ArrayList<EsameDato>();


public void iscriviStudente(String nomeStudente, String cognomeStudente, CorsoStudi corsoStudente){
    listaStudenti.add(new Studente(nomeStudente, cognomeStudente, counterStudenti, corsoStudente));
    counterStudenti++;
}

public void reimmatricolaStudente(int unaMatricola, int unIdCorso){
    int c = 0;
    CorsoStudi unCorso = null;
    for(c = 0; c < listaCorsi.size(); c++){
        if(listaCorsi.get(c).getIdCorso() == unIdCorso)
            unCorso = listaCorsi.get(c);
    }
    int contatore = 0;
    for(contatore = 0; contatore < listaStudenti.size(); contatore++)
        if(listaStudenti.get(contatore).getMatricola() == unaMatricola){
            iscriviStudente(listaStudenti.get(contatore).getNome(), listaStudenti.get(contatore).getCognome(), unCorso);
            rimuoviStudente(unaMatricola);
        };
}

public void rimuoviStudente(int matricola){
    int contatore;
    for(contatore = 0; contatore < listaStudenti.size(); contatore++){
        if(listaStudenti.get(contatore).getMatricola() == matricola)
            listaStudenti.remove(contatore);
    }
}

public Materia creaMateria(String nomeMateria, int crediti){
    listaMaterie.add(new Materia( nomeMateria, crediti, counterMaterie));
    counterMaterie++;
    return listaMaterie.get(counterMaterie - 1);
}

public void creaCorsoStudi(String nomeCorso, ArrayList<Materia> materieCorso){
    CorsoStudi q = new CorsoStudi( nomeCorso, materieCorso, counterCorsi);
    listaCorsi.add(q);
    counterCorsi++;
}

public ArrayList<Studente> cercaStudente(int opzione, String pattern){
    int contatore = 0;
    ArrayList<Studente> listaRicercati = new ArrayList<Studente>();
    //opzione 1 = ricerca per nome
    if(opzione == 1)
        for(contatore = 0; contatore < listaStudenti.size(); contatore++){
            if(listaStudenti.get(contatore).getNome().equalsIgnoreCase(pattern))
                listaRicercati.add(listaStudenti.get(contatore));
        };
    //opzione 2 = ricerca per cognome
    if(opzione == 2)
        for(contatore = 0; contatore < listaStudenti.size(); contatore++){
            if(listaStudenti.get(contatore).getCognome().equalsIgnoreCase(pattern))
                listaRicercati.add(listaStudenti.get(contatore));
    };
    //opzione 3 = ricerca per matricola
    if(opzione == 3)
        for(contatore = 0; contatore < listaStudenti.size(); contatore++){
            if(listaStudenti.get(contatore).getMatricola() == Integer.parseInt(pattern))
                listaRicercati.add(listaStudenti.get(contatore));
    };
    //opzione 4 = ricerca per corsoStudi
    if(opzione == 4)
        for(contatore = 0; contatore < listaStudenti.size(); contatore++){
            if(listaStudenti.get(contatore).getCorsoStudi().getIdCorso() == Integer.parseInt(pattern))
                listaRicercati.add(listaStudenti.get(contatore));
    };

    return listaRicercati;
}

public Materia materiaDaId(int id){
    int c = 0;
    Materia materiaDaRitornare = null;
    for(c = 0; c < listaMaterie.size(); c++){
        if(listaMaterie.get(c).getIdMateria() == id)
            materiaDaRitornare = listaMaterie.get(c);
    }
    return materiaDaRitornare;
}
}

CorsoStudi.java

import java.util.ArrayList;
import java.util.Scanner;

public class CorsoStudi {

private String nomeCorso;
private int idCorso;
private ArrayList<Materia> elencoMaterie;
private ArrayList<EsameDato> elencoEsamiDati;

public CorsoStudi(String unNomeCorso, ArrayList<Materia> unElencoMaterie, int unIdCorso){
    nomeCorso = unNomeCorso;
    elencoMaterie = unElencoMaterie;
    idCorso = unIdCorso;
    int contatore = 0;
    //EsameDato q = null;
    for(contatore = 0; contatore < unElencoMaterie.size(); contatore++){
        EsameDato q = new EsameDato(unElencoMaterie.get(contatore), 0);
        elencoEsamiDati.add(q);
    };
}

public String getNomeCorso(){
    return nomeCorso;
}

public int getIdCorso(){
    return idCorso;
}

public ArrayList<Materia> getElencoMaterie(){
    return elencoMaterie;
}

public ArrayList<EsameDato> getElencoEsamiDati(){
    return elencoEsamiDati;
}

public String toString(){
    String s = "";
    s = s + "Ecco le materie di questo Corso di Studi:\n";
    int c = 0;
    for(c= 0; c < elencoMaterie.size(); c++){
        s = s + elencoMaterie.get(c).getIdMateria() + " "; 
        s = s + elencoMaterie.get(c).getNomeMateria() + " (";
        s = s + elencoMaterie.get(c).getCrediti() + " crediti)\n";
    }
    return s;
}
}
8
  • 1
    "Eclipse doesn't advice error on writing code." -- because NPE is a run time exception. Commented Jul 28, 2012 at 14:13
  • dumping all your code in (I guess) Spanish won't help. Please translate the relevant parts and set up an SSCCE: sscce.com Commented Jul 28, 2012 at 14:15
  • OP doesn't have to translate this from Italian. Variables are variables, functions are functions. What's the difference if they're in one language versus another - it shouldn't be difficult to tell what's going on. Commented Jul 28, 2012 at 14:17
  • 1
    @11684 - If you can't debug an NPE in a code written in a foreign language, you ain't a real programmer. (Care for a slice of quiche? :-) ) Commented Jul 28, 2012 at 14:17
  • sorry, .org: sscce.org Commented Jul 28, 2012 at 14:18

1 Answer 1

2

You have not initialized the field elencoEsamiDati therefore the actual value is null.

Before adding elements to an ArrayList you need to create it:

private ArrayList<EsameDato> elencoEsamiDati = new ArrayList<EsameDato>();

You may also need to initialize other fields as well.

BTW it is not a good idea to use your own language when programming, use English instead.

Sign up to request clarification or add additional context in comments.

6 Comments

There's really nothing wrong with using your own language to program. The only thing that matters is that reserved words are correct, function and class names are consistent, and the style is readable. All three here are pretty spot on.
I don't agree with you on that, it was really hard for me to even write down the name of the class.
Thanks! It was a stupid problem but I've lost an hour around it. Many thanks!
@Makoto there's nothing wrong with that if you don't expect anyone else to be able to read it. I worked with some code in German and it was less fun than you can imagine.
@Makoto yes, but that only shows the problem is really simple in this case. When you need more assistance or want to collaborate with someone, ensuring the readability of the code seems advisable to me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.