PDA

View Full Version : [JAVA] Problema test con i Thread


Villain
18-07-2011, 18:59
Dovrei testare con i thread un'applicazione che gestisce una tavola calda.
1 thread per crea 1 per deposita e 1 per eroga.

il problema è che mi torna nullpointerException su questa istruzione

if (mappaProdotti.get(k.toString())==null)

quando faccio questo controlloreCorrente.deposita(id,10);

vi prego, help me!
import java.io.File;

import javax.swing.JOptionPane;


public class ThreadInserisci implements Runnable {

private Acqua acqua;
private ControlloreGestione controlloreGestione;
private IDEsterno id;
private Orologio oggi;
private Controllore controlloreCorrente;

public ThreadInserisci(Controllore controllore, IDEsterno id){

new File("Ser").mkdir();
new File("Log").mkdir();
this.id = id;
this.controlloreCorrente=controllore;
this.acqua = acqua;
new Thread(this, "inserisci").start();
}

public void run(){
// codice del thread
try{
ControlloreGestione controlloreGestione = new ControlloreGestione();
//IDEsterno id = new IDEsterno("Ferrarelle");
//Acqua acqua = new Acqua(id);
controlloreGestione.nuovoProdotto("Acqua", id);
//IDEsterno articolo = new IDEsterno(iDEsternoProdotto.getText());
int giac= 0;
oggi=new Orologio();
SalvataggioVeloce salva = new SalvataggioVeloce();
// salva.salvaLog(articolo, oggi/*, ruolo, pass*/);
salva.salvaMenu(id, "Acqua");
salva.salvaLogErogaDeposita(id, oggi, "APPENA INSERITO", giac, "0");

}
catch(DirittiException e)
{
JOptionPane.showMessageDialog(null, " NuovoProdottoAA " + e.getMessage(), " non autorizzati", JOptionPane.ERROR_MESSAGE);
}
catch(ClasseAlimentareException e)
{
JOptionPane.showMessageDialog(null, " NuovoProdottoAA " + e.getMessage(), " Errore di tipo", JOptionPane.ERROR_MESSAGE);
}
catch(ProdottoException e)
{
JOptionPane.showMessageDialog(null, " NuiovoProdottoAA " + e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
}
catch (IDEsternoException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
}
}

}



import javax.swing.JOptionPane;


public class ThreadDeposita implements Runnable {

private Acqua acqua;
//private ControlloreDeposito controlloreDeposito;
private ControlloreGestione controlloreGestione;
private IDEsterno id;
private Orologio oggi;
private Controllore controlloreCorrente;


public ThreadDeposita(Controllore controllore, IDEsterno id){
//this.acqua = acqua;
this.id = id;
this.controlloreCorrente=controllore;
new Thread(this, "deposita").start();
}

public void run(){
// codice del thread
try
{

controlloreCorrente.deposita(id,10);
//ControlloreDeposito deposito = new ControlloreDeposito();
acqua.deposita(10);
int giac=acqua.giacenza();

// String ruolo=controlloreCorrente.utenteCorrente.getRuolo();
// String pass=controlloreCorrente.utenteCorrente.getPassword();
oggi=new Orologio();
SalvataggioVeloce salva = new SalvataggioVeloce();
salva.salvaLogErogaDeposita(id, oggi, "DEPOSITA", giac, Integer.toString(10));

}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, " DepositaAA Numero non valido. ");
}
catch(ProdottoException e)
{
JOptionPane.showMessageDialog(null, " DepositaAA " + e.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
}
catch (QuantitaException e)
{
JOptionPane.showMessageDialog(null, " DepositaAA Quantità non valida. ");
}
catch (IDEsternoException e)
{
JOptionPane.showMessageDialog(null, " DepositaAA Nome prodotto non non valido. ");
}
catch(DirittiException e)
{
JOptionPane.showMessageDialog(null, " DepositaAA " + e.getMessage(), "non autorizzato", JOptionPane.ERROR_MESSAGE);
}


}


}



public void deposita(IDEsterno k, int quantita) throws DirittiException, ProdottoException, IDEsternoException, QuantitaException
{
if (k==null)
{
throw new IDEsternoException("L'identificatore del prodotto deve essere non nullo");
}
if (mappaProdotti.get(k.toString())==null)
{
throw new ProdottoException("Prodotti " + k.toString() + " non sono presenti");
}
else
{
if (quantita <=0) {
throw new QuantitaException ("La quantita da depositare deve essere positiva\n");
}
else
mappaProdotti.get(k.toString()).deposita(quantita);
}
}

banryu79
19-07-2011, 09:10
Ciao, dal codice che hai postato direi che se viene lanciata una NullPointerException in questa riga:

if (mappaProdotti.get(k.toString())==null)

le uniche due cause possibili per cui questo accade sono:
1) mappaProdotti è null
2) il tipo di mappa usato non permette elementi null e l'argomento passato k (IDEsterno) è null

tylerdurden83
19-07-2011, 13:35
Anche se il tipo di mappa usato permettesse valori null, chiamare k.toString con k null darebbe comunque una nullPointerException. Tuttavia questo non può essere il caso, dato che c'è un if(k==null) prima dell'istruzione incriminata, quindi k non è null, quindi l'unica possibilità è che non hai inizializzato mappaProdotti a qualcosa che non sia null.

banryu79
19-07-2011, 13:44
Anche se il tipo di mappa usato permettesse valori null, chiamare k.toString con k null darebbe comunque una nullPointerException.
Yep, mi era sfuggito.

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.