1

right now I have to read/write an array of objects to a binary file. I have the code in place, but for some reason I am getting a write error (an error that I created in my try catch). Any solutions for how to fix this are appreciated. Thanks.

public class Trivia {
private String question;
private String answer;
private int points;

public Trivia() {
    question = " ";
    answer = " ";
    points = 0;
}

public String getQuestion() {
    return question;
}

public String getAnswer() {
    return answer;
}

public int getPoints() {
    return points;
}

public void setQuestion(String q) {
    question = q;
}

public void setAnswer(String a) {
    answer = a;
}

public void setPoints(int p) {
    points = p;
}

}

import java.io.*;
import java.util.*;

public class Driver  {

public static void main(String[] args) {
    String fileName = "trivia.dat";
    Trivia[] t = new Trivia[5];
    for (int i = 0; i < 5; i++) {
        t[i] = new Trivia();
    }

    t[0].setQuestion("How many states are in the US?");
    t[0].setAnswer("50");
    t[0].setPoints(1);

    t[1].setQuestion("What is the capital of Michigan?");
    t[1].setAnswer("Lansing");
    t[1].setPoints(1);

    t[2].setQuestion("How many senators come from each state?");
    t[2].setAnswer("2");
    t[2].setPoints(2);

    t[3].setQuestion("What is the largest state?");
    t[3].setAnswer("Alaska");
    t[3].setPoints(2);

    t[4].setQuestion("Who was the thrid president?");
    t[4].setAnswer("Thomas Jefferson");
    t[4].setPoints(3);

    ObjectOutputStream outputStream = null;

    try{
        outputStream = new ObjectOutputStream(new FileOutputStream("trivia.dat"));

    }catch(IOException e){
        System.out.println("Could not open file");
        System.exit(0);
    }

    try{
        outputStream.writeObject(t);
        outputStream.close();
    }catch(IOException e){
        System.out.println("Writing error");
        System.exit(0);
    }

    ObjectInputStream inputStream = null;

    try{
        inputStream = new ObjectInputStream(new FileInputStream("trivia.dat"));

    }catch(IOException e){
        System.out.println("File not found.");
        System.exit(0);
    }
     Trivia[] test = null;

     try{
         test = (Trivia[])inputStream.readObject();
     }catch(Exception e){
         System.out.println("Reading error");
         System.exit(0);
     }



}
}
4
  • And the exception is...? Commented Mar 1, 2016 at 23:06
  • Show us some stack trace Commented Mar 1, 2016 at 23:06
  • I said it above. I am getting "Writing error". It's already in the code }catch(IOException e){ System.out.println("Writing error"); System.exit(0); } Commented Mar 1, 2016 at 23:08
  • 2
    The good thing about exceptions is that they contain an error message and a stack trace. You should use this information like e.printStackTrace() to print the stack trace to the console. So please change your code and try to understand the error message. Maybe this solve your problem directly. Otherwise post the stacktrace here and mark the line, where the exception is thrown. Commented Mar 1, 2016 at 23:11

2 Answers 2

2

Your Trivia class has to implement the Serializable interface to allow writing it using an ObjectInputStream.

Sign up to request clarification or add additional context in comments.

8 Comments

Same thing, after implementing it I still get my "Writing error"\
Really? Hm. I couldn't tell you what's going on. This is what I changed it to. public class Driver implements Serializable
@ProgrammingGuy123 I said Trivia has to implement the interface NOT Driver ;-)
Ah, so no more errors anymore. But, now that it actually runs, my trivia.dat file is still empty even after it is supposed to have been written to.
How did you check that it's empty?
|
0

Always do a printStackTrace() in your catch clause to understand what's going on.

You may have understand by yourself that your Trivia class does not implement java.io.Serializable.

1 Comment

"Always do a printStackTrace()" or just don't catch, let it bubble up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.