Pagina 2 di 3 primaprima 1 2 3 ultimoultimo
Visualizzazione dei risultati da 11 a 20 su 21
  1. #11
    Originariamente inviato da ELY85
    Ho aggiunto alla classe ListArray l'interfaccia Serializable...

    codice:
    public class ListArray implements List implements Serializable {
    
                       .....  }
    ma il compilatore mi dà questo errore...proprio nella prima riga di codice dove implemento Serializable: '{' expected

    Che significa?
    GRAZIE

    Piccolo errore

    codice:
    public class ListArray implements List, Serializable {
    
                       .....  }

  2. #12
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    Grazie mille!

  3. #13
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    Il metodo visualizzaProdotto continua a non stamparmi niente!
    Non capisco il motivo...!
    Il codice è corretto?
    O c'è qualcosa che non va?
    Grazie.

    METODO salvaProdotto:

    [ CODE]

    public void salvaProdotto(String codice, String nome) {
    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream("Prodotti.dat", true);
    }
    catch(IOException e) {
    System.exit(1);
    }

    ObjectOutputStream os = null;
    Prodotto p = new Prodotto(codice, nome);
    try {
    os = new ObjectOutputStream(fos);
    os.writeObject(p);
    os.flush();
    os.close();
    }
    catch (IOException e) {
    System.exit(2);
    }
    }

    [/ CODE]


    METODO visualizzaProdotto:

    [ CODE]

    public void visualizzaProdotto() {
    FileInputStream fis = null;
    ObjectInputStream is = null;
    try {
    fis = new FileInputStream("Prodotti.dat");
    is = new ObjectInputStream(fis);
    }
    catch (IOException e) {
    System.exit(3);
    }

    Prodotto p2 = null;
    try {
    p2 = (Prodotto) (is.readObject());
    is.close();
    }
    catch (IOException e) {
    System.exit(4);
    }
    catch (ClassNotFoundException e) {
    System.exit(5);
    }

    System.out.println(p2.getNome());

    }

    [/ CODE]

  4. #14
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Dicendo "Non mi funziona" non dai troppe indicazioni. Al posto delle System.exit(); metti dei bei e.printStackTrace() e posta l'eccezione che ti solleva (se te ne solleva una).

    P.s. Quando posti il codice metti [CODE] senza spazi, non [ CODE], altrimenti e' inutile che tu lo metta.

  5. #15
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    Ora non mi dà errori, però io ho inserito un Prodotto A, e mi ha visualizzato un altro Prodotto che avevo inseriro prima!
    Perchè non me li fa vedere tutti?
    GRAZIE

  6. #16
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    Adesso se inserisco due prodotti me ne stampa 2 ma due uguali. Il prodotto che inserisco per primo me lo stampa 2 volte!

    Comunque ho già trovato una cosa da modificare!
    Al metodo salvaProdotto passo come parametri due stringhe(nome e codice), ma non va bene perchè mi salva solo nome e codice!
    Io voglio che mi salvi l'intero oggetto che ho creato con tutti i suoi attributi.
    Quindi a questo metodo devo passare come parametro l'oggetto Prodotto che ho creato!
    Giusto?
    Ma come va modificato quindi il codice dei due metodi salvaProdotto e visualizzaProdotto?

    GRAZIE!

  7. #17
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Veramente tu scrivi gia' un prodotto, quindi la scrittura e' ok. Hai solo quei due parametri e quindi ti salva ovviamente quei due parametri. Nel codice che hai postato leggi un solo prodotto, quindi giustamente ti visualizza il primo e solo quello.
    Cmq io sposterei i metodi per leggere/scrivere file in una classe apposita.
    Fai un metodo Scrivi che prende in input un array/lista/qualsiasiCosaTiPare, la scorre e salva su file ogni oggetto e un metodo Leggi che legge tutto il file e restituisce un array/lista/qualsiasiCosaTiPare di prodotti. I metodi andranno chiamati leggi all'apertura del programma e scrivi alla chiusura.
    Finche' il programma e' a runTime io non starei a scrivere e leggere da file ogni volta.
    Esempio lettura di piu' oggetti.
    codice:
    ArrayList prodotti = new ArrayList();
    FileInputStream input = null;
    try {
       input = new ObjectInputStream(new FileInputStream("Prodotti.dat"));
       Prodotto p;
       while (true) {
          p = (Prodotto) reader.readObject();
          prodotti.add(obj);
       }   
    } catch (IOException e1) {
       e.printStackTrace();
    } catch (EOFException e2) {//SONO ALLA FINE DEL FILE, MI SERVE PER FERMARE IL WHILE}
    input.close();

  8. #18
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    Grazie per il suggerimento e per gli aiuti che mi hai dato!

  9. #19
    Utente di HTML.it
    Registrato dal
    Mar 2005
    Messaggi
    225
    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!

  10. #20
    Utente di HTML.it L'avatar di Ed_Bunker
    Registrato dal
    Jul 2003
    Messaggi
    1,119
    L'errore chiaro: invochi un metodo su un ogetto "null" o passi un oggetto null a qualche metodo.

    Il problema e' capire la CAUSA del malfunzionamento.
    E riportare 900 righe di codice nella speranza che qualcuno le legga e simuli la jvm non e' propriamente la cosa migliore...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.