Reading/Writing from an object

  • Thread starter Andrew Thompson
  • Start date
A

Andrew Thompson

I am new to Java ..

You'd best advised to post to a
different group for the moment.
I have another class VideoStore that creates a form with some buttons for
New/Add.First/Next. ...
I have pasted the code from both my classes into this post in the hopes
that somebody can point me in the right direction.

Posting code is good, but you need a good,
long look over how to prepare code for others
to see. I tried compiling and running your
example but it did not work for various reasons,
most of which are mentioned here..
<http://www.physci.org/codes/sscce.jsp>

I will, however, address particular aspects
of the code..
*************************************************************
//videoRecord CLASS
/*
* Video.java
* Created on 25 April 2004, 16:04
*/
package VideoStore.VideoRecord;

remove package statements from examples,
but never put Caps in package names..
import java.io.*;
public class videoRecord implements Serializable{

Class names should have every word
begin with a Capital, so that would be
a fully qualified name of..

videostore.videorecord.VideoRecord
( package ) ( class )

Then we come to..
org.netbeans.lib.awtextra.AbsoluteLayout()

Aaaaargh! This is not the way to code a GUI,
not that htis test even requires a GUI.

Strip the entire GUI from it, and avoid posting
code that uses external resources unless *absolutely*
necessary.

See you over on c.l.j.help (but please
read that document on the SSCCE and gut
your code first..)

BTW - Sun's I/O tutorial may contain the tip you need..
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>

HTH
 
A

Allen

Hi there,

I am new to Java and have a small problem that I hope somebody can help me
with. I have a class videoRecord that is used to hold data about videos in
a rental libraray. It reads this data from a text file (video.txt)

I have another class VideoStore that creates a form with some buttons for
New/Add.First/Next.

When I fire up the app I can add a new record. If I then clcik on First it
shows this record. If I then add another record and click on First it only
shows this one. The original record has been overwritten by the second one.

I have pasted the code from both my classes into this post in the hopes
that somebody can point me in the right direction.

Thanks in advance.

*************************************************************
//videoRecord CLASS
/*
* Video.java
* Created on 25 April 2004, 16:04
*/
package VideoStore.VideoRecord;
import java.io.*;
public class videoRecord implements Serializable{

private String classification;
private String title;
private float duration;
private float hireCost;

/** Creates a new instance of VideoRecord */
public videoRecord() {
this("","",0,0);
}
public videoRecord(String c, String t, float d, float h) {
setClassification(c);
setTitle(t);
setDuration(d);
setHireCost(h);
}
public void setClassification(String c){
classification = c;
}
public String getClassification(){
return classification;
}
public void setTitle(String t){
title = t;
}
public String getTitle(){
return title;
}
public void setDuration(float d){
duration = d;
}
public float getDuration(){
return duration;
}
public void setHireCost(float h){
hireCost = h;
}
public float getHireCost(){
return hireCost;
}
}
*************************************************************

*************************************************************
//VideoStore CLASS

/*
* VideoStore.java
*
* Created on 16 June 2004, 13:51
*/

package VideoStore.VideoRecord;

import java.io.*;
import javax.swing.*;

/**
*
* @author allenjones
*/
public class VideoStore extends javax.swing.JFrame {
ObjectInputStream input;
File file = new File(new
File(getClass().getResource("data").getFile()),"video.txt");

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

/** 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.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtClassification = new javax.swing.JTextField();
txtTitle = new javax.swing.JTextField();
txtDuration = new javax.swing.JTextField();
txtHireCost = new javax.swing.JTextField();
cmdNew = new javax.swing.JButton();
cmdAdd = new javax.swing.JButton();
cmdFirst = new javax.swing.JButton();
cmdNext = new javax.swing.JButton();

getContentPane().setLayout(new
org.netbeans.lib.awtextra.AbsoluteLayout());

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jLabel1.setText("Class");
getContentPane().add(jLabel1, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 50, 20));

jLabel2.setText("Title");
getContentPane().add(jLabel2, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 50, 20));

jLabel3.setText("Duration");
getContentPane().add(jLabel3, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, 50, 20));

jLabel4.setText("Hire Cost");
getContentPane().add(jLabel4, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 50, 20));

getContentPane().add(txtClassification, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 90, -1));

getContentPane().add(txtTitle, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 90, -1));

getContentPane().add(txtDuration, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, 90, -1));

getContentPane().add(txtHireCost, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, 90, -1));

cmdNew.setText("New");
cmdNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdNewActionPerformed(evt);
}
});

getContentPane().add(cmdNew, new
org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, -1, -1));

cmdAdd.setText("Add");
cmdAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdAddActionPerformed(evt);
}
});

getContentPane().add(cmdAdd, new
org.netbeans.lib.awtextra.AbsoluteConstraints(270, 50, -1, -1));

cmdFirst.setText("First");
cmdFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdFirstActionPerformed(evt);
}
});

getContentPane().add(cmdFirst, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, -1, -1));

cmdNext.setText("Next");
cmdNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdNextActionPerformed(evt);
}
});

getContentPane().add(cmdNext, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, -1, -1));

java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-348)/2, (screenSize.height-228)/2, 348,
228);
}

private void cmdNextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

videoRecord record;

try {
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification());
txtTitle.setText(record.getTitle());
txtDuration.setText(Float.toString(record.getDuration()));
txtHireCost.setText(Float.toString(record.getHireCost()));
}

catch (EOFException e)
{
clearTexts();
cmdNext.setEnabled(false);
}

catch (Exception a)
{
JOptionPane.showMessageDialog(this, "Error here", "Error",
JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}

}

private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

videoRecord record;

try {
input = new ObjectInputStream(new FileInputStream(file));
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification());
txtTitle.setText(record.getTitle());
txtDuration.setText(Float.toString(record.getDuration()));
txtHireCost.setText(Float.toString(record.getHireCost()));
cmdNext.setEnabled(true);
}

catch (IOException e)
{
JOptionPane.showMessageDialog(this, "Can't find record",
"Error", JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}

catch (ClassNotFoundException e)
{
JOptionPane.showMessageDialog(this, "Can't find record",
"Error", JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}

}

private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

videoRecord record;

try {
record = new videoRecord(
txtClassification.getText(),
txtTitle.getText(),
Float.parseFloat(txtDuration.getText()),
Float.parseFloat(txtHireCost.getText()));

FileOutputStream ostr = new FileOutputStream(file);
ObjectOutputStream oostr = new ObjectOutputStream(ostr);
oostr.writeObject(record);

//oostr.close(); // may be necessary to complete the write

clearTexts();
}

catch (IOException e)
{
JOptionPane.showMessageDialog(this, "Something wrong!",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}


}

private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

try {

clearTexts();
}

catch (Exception e){
JOptionPane.showMessageDialog(this, "Something wrong!", Error",
JOptionPane.ERROR_MESSAGE);
//cmdAdd.setEnabled(false);
e.printStackTrace();
}
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new VideoStore().show();
}

public void clearTexts()
{
txtClassification.setText("");
txtTitle.setText("");
txtDuration.setText("");
txtHireCost.setText("");

}
// Variables declaration - do not modify
private javax.swing.JButton cmdAdd;
private javax.swing.JButton cmdFirst;
private javax.swing.JButton cmdNew;
private javax.swing.JButton cmdNext;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField txtClassification;
private javax.swing.JTextField txtDuration;
private javax.swing.JTextField txtHireCost;
private javax.swing.JTextField txtTitle;
// End of variables declaration
}
*************************************************************

--
ats@home

When an old lady got hit by a truck
I saw a wicked gleam in your eye

Handy utils here: http://www.allenjones.co.uk/utils.htm
 
R

Roedy Green

remove package statements from examples,
but never put Caps in package names..

I think that is unwise. What if the problem IS the package statement.

I sometimes do that when I quote code I have written for clients to
protect their privacy.
 
A

Allen

You'd best advised to post to a
different group for the moment.
<http://www.physci.org/codes/javafaq.jsp#cljh>

Thanks for the redirect. I will find time for a visit.
Posting code is good, but you need a good,
long look over how to prepare code for others
to see. I tried compiling and running your
example but it did not work for various reasons,
most of which are mentioned here..
<http://www.physci.org/codes/sscce.jsp>

I will, however, address particular aspects
of the code..
remove package statements from examples,
but never put Caps in package names..

The package code comes from creating a java package in Netbeans. And the
caps I put in just to show where the classes start and end
Class names should have every word
begin with a Capital, so that would be
a fully qualified name of..

videostore.videorecord.VideoRecord
( package ) ( class )

I'll remember that one.
Then we come to..

Aaaaargh! This is not the way to code a GUI,
not that htis test even requires a GUI.

Again that comes from using Netbeans.
Strip the entire GUI from it, and avoid posting
code that uses external resources unless *absolutely*
necessary.

See you over on c.l.j.help (but please
read that document on the SSCCE and gut
your code first..)

Will do that over the next day or two.
BTW - Sun's I/O tutorial may contain the tip you need..
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>

Off to that link now

Thanks for your reply.

--
ats@home

When an old lady got hit by a truck
I saw a wicked gleam in your eye

Handy utils here: http://www.allenjones.co.uk/utils.htm
 
A

Andrew Thompson

I think that is unwise. What if the problem IS the package statement.

Would you be offended if I responded,
"well, d'uh"? ;-)

OK, more seriously. If it *is* the package
statement, if removing the package statement
causes the code to *work*, the OP has already
made great strides towards solving the problem.

They can then return to the group with
'having trouble with packages'..

The point of the SSCCE though (the context
in which that comment was made) was that you
snip code 'bit-by-bit' back to the simplest
that displays the problem. That in itself
often reveals the answer, and if not, you
have a short example to display for others.
It makes a great deal of sense.
 
R

Roedy Green

They can then return to the group with
'having trouble with packages'..


Dreamer. What will happen in practice is everyone will say the code
works ok, and the OP will still be puzzled why is unmodified version
does not work. He will then be sent off on a goose chase reinstalling
his tools.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top