Java writeObject i readObject

0

Witam,
załączona część programu ma zapisywać i wczytywać pliki. Dlaczego linijki, które są aktualnie zakomentowane wyrzucają błąd?

 private void bSaveActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        File file;
        JFileChooser FCS = new JFileChooser();
        if (FCS.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            file = FCS.getSelectedFile();
            JOptionPane.showMessageDialog(null, "Selected file:" + file.getAbsolutePath());
            try {
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream o = new ObjectOutputStream(fos);
                o.write(s);
                for (int i = 0; i < s; i++) o.writeObject(Square[i]);
                o.write(r);
                for (int i = 0; i < r; i++) o.writeObject(Rectangle[i]);
                o.write(c);
                for (int i = 0; i < c; i++) o.writeObject(Circle[i]);
                o.write(p);
                for (int i = 0; i < p; i++) o.writeObject(Polygon[i]);
                o.close();
                fos.close();
            } 
            catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, "Nie utworzono pliku", null, JOptionPane.PLAIN_MESSAGE);
            } 
            catch (IOException e) {
                System.out.println("Error");
            }
        }
    }                                     

    private void bOpenActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        File file;
        JFileChooser FCO = new JFileChooser();
        if (FCO.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            file = FCO.getSelectedFile();
            JOptionPane.showMessageDialog(null, "Selected file:" + file.getAbsolutePath());
           try {
                FileInputStream fosi = new FileInputStream(file);
                ObjectInputStream oi = new ObjectInputStream(fosi);
                //for (int i = 0; i < s; i++) oi.readObject(Square[i]);
               //for (int i = 0; i < r; i++) oi.readObject(Rectangle[i]);
                //for (int i = 0; i < c; i++) oi.readObject(Circle[i]);
                //for (int i = 0; i < p; i++) oi.readObject(Polygon[i]);
                oi.close();
                fosi.close();
            } 
            catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, "Nie utworzono pliku", null, JOptionPane.PLAIN_MESSAGE);
            } 
            catch (IOException e) {
                System.out.println("Error");
            }
        }
    }  
    
0

Metoda readObject jest bezargumentowa. Chyba chciałeś napisać

Square[i] = oi.readObject()

1 użytkowników online, w tym zalogowanych: 0, gości: 1