0

I'm making a GUI program, using netbeans, that is supposed to be an interface for managing records in a video store.

enter image description here enter image description here

This is the interface. It's two tabs, and one side allows a person to add records, whilst the other tab displays them. When a person adds records, they are added to a .dat file named output. I would like to use the .dat file as a permanent storage area for the video records, and basically what I want to happen is that when one loads the GUI class, the program loads all the records from the .dat file. I have already created my code, but I'm getting the following error:

run:
java.io.EOFException
    at java.io.RandomAccessFile.readChar(RandomAccessFile.java:773)
    at videostore.BinaryFile.getString(BinaryFile.java:82)
    at videostore.BinaryFile.load(BinaryFile.java:116)
    at videostore.VideoStore.main(VideoStore.java:409)
Exception in thread "main" java.lang.NullPointerException
    at videostore.VideoStore.main(VideoStore.java:420)
/Users/(my Name)/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)

And then I'll paste all relevant code below.

In the main method of the GUI class, named VideoStore.java:

file = new BinaryFile("/Users/hanaezz/Desktop/output.dat");
        int length = file.length();
        int ID = 1;

        for (int xx = 0; xx < length; xx += file.getRecordLength()) {
            Video load = file.load(xx);
            String tempName = load.getVideoName();
            String tempProd = load.getProducer();
            String tempRat = load.getRating();
            String tempGenre = load.getGenre();
            short tempNum = load.getVidNum();
            float tempPrice = load.getvideoPrice(); 

            Object[] row = {ID, tempName, tempProd, tempGenre, tempRat, tempNum, tempPrice};


            model.addRow(row);

            ID++;
        }

in the VideoStore constructor class:

public VideoStore() {
        initComponents();
        model = (DefaultTableModel) displayVideos.getModel();
    }

And within the BinaryFile class:

private static final int RecordLength = 112;
public static Video load(int place){
        String name = "", prod="", rat="", genre="";

        float price = 1;
        short number = 1;
        try {
            raf.seek(place);
            name = getString(20);
            prod = getString(15);
            rat = getString(20);
            genre = getString(10);
            price = Float.parseFloat(getString(4));
            number = Short.parseShort(getString(4));

            writeString(20, name);
            writeString(15, prod);
            writeString(10, genre);
            writeString(4, VideoStore.vPrice.getText());
            writeString(4, VideoStore.vNumber.getText());
            writeString(4, rat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Video r = new Video(name, prod, genre, rat, number, price);
        return r;
    }

    public static int getRecordLength() throws IOException{
        return RecordLength;
    }

    public static int length() throws IOException {
        return (int)raf.length();
        }

And finally, my Video class:

    private static String videoName;
    private static String producer;
    private static String rating;
    private static String genre;
    private static short videoNumber;
    private static float videoPrice;

public Video(String a, String b, String c, String d, short e, float f){
    videoName = a;
    producer = b;
    rating = c;
    genre = d;
    videoNumber = e;
    videoPrice = f;
}

...Then mutator and accessor methods for each variable in the class...

@Override
public String toString(){
    return videoName + "\t" + producer +
            "\t" + rating + "\t" + genre +
            "\t" + videoNumber + "\t" + videoPrice;
}

So yeah, my issue is that I can't figure out how to load records from the file into the table. In my code I tried to use a loop that would iterate through each record in the file based on the record's size.. however it doesn't seem to have worked. If anyone would like to see my full code or needs more information, don't hesitate to contact me :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
blue sky
  • 161
  • 1
  • 13

1 Answers1

1

First, you should use a more Object Oriented approach.

Your video class contains nothing but static attributes, when it should looks like this:

public class Video implements Serializable{

    private String name;
    private String producer; //consider using an object for this
    private String rating; //consider using a numeric type for this
    private String genre; //consider using an object for this
    private int number;
    private double price;


    //getters and setters

}

Check the Object-Oriented Programming Concepts.

To add a new video, you get the user input from the graphic interface, and use it to create a Video object.

Video video = new Video();

video.setName(nameTextField.getText());
//same for the other attributes

Then you can keep all your videos in a List.

List<Video> videosList = new ArrayList<>();

videoList.add(video);

Then you can serialize your list to a file.

try(FileOutputStream outputFile = new FileOutputStream("/path/to/file");
    ObjectOutputStream out = new ObjectOutputStream(outputFile)){

      out.writeObject(videoList);

} catch (IOException e1) {

      // Handle the exception

}

To read back your list from the file, you need to deserialize it:

try(FileInputStream inputFile = new FileInputStream("/path/to/file");
    ObjectInputStream in = new ObjectInputStream(inputFile)){

      videoList = (List<Video>)in.readObject();

} catch (IOException e1) {

      // Handle the exception

}
Jefferson Lima
  • 5,186
  • 2
  • 28
  • 28
  • i appreciate your help! however i'm not sure this answers my question... how do i load the file back into the jTable once i reboot the program? – blue sky Apr 08 '16 at 14:09
  • @Hanalei: It depends on how you wrote the file. If you follow Jefferson Lima's instructions, you read the file and deserialize it. – Gilbert Le Blanc Apr 08 '16 at 15:14
  • @GilbertLeBlanc okay.. but if i serialize all the objects into one file, then won't that make it very difficult for me to un-serialize it and feed it back into the table? like will the compiler know how to differentiate between the different objects? – blue sky Apr 08 '16 at 15:53
  • @Hanalei: Try serializing a List of Video objects, and deserializing the List. The people that wrote the serializing code were pretty smart. – Gilbert Le Blanc Apr 08 '16 at 16:03
  • I added an example of how you can serialize and deserialize the list. – Jefferson Lima Apr 11 '16 at 13:55