Sono di nuovo ferma!
Non riesco a capire cosa c'è che non va!
Errore che mi segnala il compilatore:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PanDomanda.actionPerformed(PanDomanda.java:56)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
Ora vi mando il codice utile.
CLASSE PanDomanda:
codice:
public class PanDomanda extends JPanel implements ActionListener{
private JButton b1, b2;
private JLabel jl;
public PannelloProdotto riferimento;
public ScriviLeggiProdotti rifer;
public PanDomanda(PannelloProdotto chiamante, ScriviLeggiProdotti chi) {
super();
setBackground(new Color(255,255,0));
riferimento = chiamante;
rifer = chi;
jl = new JLabel("Desidera inserire altri prodotti?");
add(jl);
b1 = new JButton("Sì");
b2 = new JButton("No");
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
riferimento.jtfCodice.setText("");
riferimento.jtfNome.setText("");
riferimento.jtfNumIng.setText("");
riferimento.txt1.setText("");
riferimento.txt2.setText("");
riferimento.txt3.setText("");
riferimento.findom.dispose();
}
if(e.getSource()==b2) {
rifer.slp.scriviProdotti(riferimento.p.getListaProdotti());
rifer.slp.leggiProdotti();
riferimento.findom.dispose();
}
}
}
CLASSE PannelloProdotto:
codice:
public class PannelloProdotto extends JPanel implements ActionListener {
private JLabel codice, nome, ingredienti;
public JTextField jtfCodice, jtfNome, jtfNumIng, txt1, txt2, txt3;
private JButton b1, b2;
private Prodotto rif;
public Prodotto p;
public FinestraIngredienti f;
public FinDomanda findom;
public ScriviLeggiProdotti rifer;
public PannelloProdotto() {
super();
setBackground(new Color(185,220,255)); // Azzurro chiaro.
codice = new JLabel("Codice prodotto: ");
nome = new JLabel("Nome prodotto: ");
ingredienti = new JLabel("Numero di ingredienti: ");
jtfCodice = new JTextField(5);
jtfNome = new JTextField(7);
jtfNumIng = new JTextField(5);
txt1 = new JTextField(5);
txt2 = new JTextField(7);
txt3 = new JTextField(5);
jtfCodice.setEditable(true);
jtfNome.setEditable(true);
jtfNumIng.setEditable(true);
txt1.setEditable(false);
txt2.setEditable(false);
txt3.setEditable(false);
jtfCodice.addActionListener(this);
jtfNome.addActionListener(this);
jtfNumIng.addActionListener(this);
b1 = new JButton("INSERISCI INGREDIENTI");
b2 = new JButton("SALVA");
b1.addActionListener(this);
b2.addActionListener(this);
setLayout(new FlowLayout(FlowLayout.LEFT,5,30));
add(codice);
add(jtfCodice);
add(txt1);
add(nome);
add(jtfNome);
add(txt2);
add(ingredienti);
add(jtfNumIng);
add(txt3);
add(b1);
add(b2);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
p = new Prodotto(jtfCodice.getText(), jtfNome.getText());
int b = Integer.parseInt(jtfNumIng.getText());
p.setNumeroIng(b);
f = new FinestraIngredienti("AGGIUNGI INGREDIENTI");
Container cont = f.getContentPane();
PannelloIngredienti pan = new PannelloIngredienti(this, rif);
cont.add(pan);
f.setVisible(true);
}
if(e.getSource() == b2) {
p.creaListaProdotti(p);
p.salvaNome(p);
findom = new FinDomanda();
Container cont = findom.getContentPane();
PanDomanda pan = new PanDomanda(this, rifer);
cont.add(pan);
findom.setVisible(true);
}
if(e.getSource() == jtfCodice) txt1.setText(jtfCodice.getText());
if(e.getSource() == jtfNome) txt2.setText(jtfNome.getText());
if(e.getSource() == jtfNumIng) txt3.setText(jtfNumIng.getText());
}
}
CLASSE ScriviLeggiProdotti:
codice:
public class ScriviLeggiProdotti extends Prodotto {
public ScriviLeggiProdotti rifer;
public ScriviLeggiProdotti slp;
public ScriviLeggiProdotti() {
super();
/**
* Richiamo il costruttore di Prodotto.
*/
slp = new ScriviLeggiProdotti();
}
/**
* Questo metodo salva sul file "Prodotti.dat" tutti i prodotti che l'utente ha
* inserito nel programma.
*/
public void scriviProdotti(ListArray l) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("Prodotti.dat");
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Errore creazione file");
System.exit(1);
}
ObjectOutputStream os = null;
try{
os = new ObjectOutputStream(fos);
os.writeObject(l.head());
os.writeObject(l.tail());
os.flush();
os.close();
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Errore scrittura");
System.exit(2);
}
}
/**
* Questo metodo legge dal file "Prodotti.dat" i prodotti che l'utente ha inserito
* nel programma e che sono stati precedentemente salvati.
*/
public void leggiProdotti() {
ListArray pro = new ListArray();
FileInputStream fis = null;
ObjectInputStream is = null;
try {
fis = new FileInputStream("Prodotti.dat");
is = new ObjectInputStream(fis);
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Errore 3");
System.exit(3);
}
Prodotto p;
try {
while (true) {
p = (Prodotto) (is.readObject());
pro.insert(p);
is.close();
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Errore lettura");
System.exit(4);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Errore 5");
System.exit(5);
}
System.out.println(pro.toString());
}
}
CLASSE Prodotto:
codice:
public class Prodotto implements Serializable {
private String codice, nome;
public ListArray INGREDIENTI;
public ListArray PRODOTTI;
public String[] prodotti;
private int N, i;
public Prodotto(String cod, String nom) {
codice = cod;
nome = nom;
INGREDIENTI = new ListArray();
PRODOTTI = new ListArray();
}
public Prodotto() {
INGREDIENTI = new ListArray();
PRODOTTI = new ListArray();
}
public String getCodice() {
return codice;
}
public String getNome() {
return nome;
}
public void salvaNome(Prodotto p) {
String s = p.getNome();
int i=0;
prodotti[i] = s;
i++;
}
public String[] dammiNomi(){
return prodotti;
}
public void setNumeroIng(int a) {
N = a;
prodotti = new String[N];
}
public int getNumeroIng() {
return N;
}
public void insIngrediente(Ingrediente ing) {
INGREDIENTI.insert(ing);
}
public void rimIngrediente(Ingrediente i) {
INGREDIENTI.remove(i);
}
public void creaListaProdotti(Prodotto p) {
PRODOTTI.insert(p);
}
public ListArray getListaProdotti() {
return PRODOTTI;
}
}
Qualcuno sa dirmi qual è l'errore?
at PanDomanda.actionPerformed(PanDomanda.java:56) si riferisce a questa riga di codice:
codice:
rifer.slp.scriviProdotti(riferimento.p.getListaProdotti());
GRAZIE MILLE!