INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Netbeans Serialization to XML

Netbeans Serialization to XML

(OP)
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.

CODE --> 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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        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                   
} 

RE: Netbeans Serialization to XML

What errors do you get?

Cheers,
Dian

RE: Netbeans Serialization to XML

(OP)
I dont get errors. Aplication dont read data from xml file.

RE: Netbeans Serialization to XML

Is the application finding the file? Did you try debugging the application in NetBeans?

Cheers,
Dian

RE: Netbeans Serialization to XML

(OP)
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.

RE: Netbeans Serialization to XML

Is jTextArea1 null after deserialization?

Cheers,
Dian

RE: Netbeans Serialization to XML

(OP)
Yes jTextArea1 is null after deserialization. I write the same application in Eclipse and it works but in NetBeans it doesnt work....;/

RE: Netbeans Serialization to XML

I'd check a couple of things:

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

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close