Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Netbeans Serialization to XML

Status
Not open for further replies.

picia2222

Technical User
Jan 8, 2013
5
Welcome. I am writing project on assessment and must be done in the serialization of data to an XML file. I have written a sample application up and running in NetBeans I write to a file but does not work for me to read data from the file. Is there anyone able to help me? Paste the code below my application.

Java:
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JTextArea;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Admin
 */
public class okienko extends javax.swing.JFrame {

    /**
     * Creates new form okienko
     */
    public okienko() {
        initComponents();
        
    }

    private void serializujTekst() {
		// otwieranie strumienia plikowego do zapisu
	try {
			XMLDecoder in = new XMLDecoder(new FileInputStream("piosenka.xml"));
			// deserializacja obiektu "tekst" klasy "JTextArea".
			jTextArea1 = (JTextArea)in.readObject();
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
        
	}
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("serializacja");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                ramka(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void ramka(java.awt.event.WindowEvent evt) {                       
       try {
                    
                        XMLEncoder out = new XMLEncoder(new FileOutputStream("piosenka.xml"));
			// serializacja obiektu "tekst" klasy "JTextArea".
			out.writeObject(jTextArea1);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
        
        
        
        System.exit(0);
    }                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see [URL unfurl="true"]http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html[/URL] 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(okienko.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(okienko.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(okienko.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(okienko.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new okienko().setVisible(true);
                okienko ser = new okienko();
                ser.serializujTekst();
               
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}
 
I dont get errors. Aplication dont read data from xml file.
 
Is the application finding the file? Did you try debugging the application in NetBeans?

Cheers,
Dian
 
I think application finding file because application write data to this file but not read data from this file. I trying debugging and everythink is good. I dont get any errors.
 
Yes jTextArea1 is null after deserialization. I write the same application in Eclipse and it works but in NetBeans it doesnt work....;/
 
I'd check a couple of things:

- The file is not empty
- The file is actually been read (FileInputStream is not null)

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top