0

I have to use the ObjectOutputStream method while using SceneBuilder in Java.

String FILENAME = "au_studentlist.txt";
    FileOutputStream outfile = new FileOutputStream(FILENAME, true);
    ObjectOutputStream outstream = new ObjectOutputStream(outfile);

    outstream.write("\n StudentID: " + txtStudentID.getText() + "\n First Name: " + txtFName.getText())

I am getting an error for the txt.getText() lines of code. The error is saying I cannot make a static reference to a non static text field.

I do not know what that means.

I don't even know if my ObjectOutputStream code is correct.

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

0

You're getting an error message because the fields you're accessing (txtStudentID and/or txtFName) belong to an instance of a class, but this is in a static method. Static methods don't have access to the fields of class instances, so you probably want to move this into an instance method. I can't be entirely sure what to suggest without seeing some more code though. Where are you trying to use ObjectOutputStream?

As a side note, you'll want to use outstream.writeUTF instead of outstream.write.

Edit Something like this should work:

public void initialize()
{
    // ...
    btnSave.setOnAction(event -> {
        String FILENAME = "au_studentlist.txt";
        FileOutputStream outfile = new FileOutputStream(FILENAME, true);
        ObjectOutputStream outstream = new ObjectOutputStream(outfile);
        outstream.writeUTF("\n StudentID: " + txtStudentID.getText() + "\n First Name: " + txtFName.getText() + "\n Last Name: " + txtLName.getText() + "\n Major: " + cboMajor.getValue() + "\n State: " + cboState.getValue() + "\n Expected Graduation Date: " + dtExpGraduationDate.getValue().toString());
    });
}
Ben Navetta
  • 457
  • 5
  • 8
  • Inside the main method of the Controller Class for Scene Builder. – Kimberly Stickley Apr 13 '15 at 22:02
  • It won't work in the main method, but it should in an instance method of the controller. You can also have it run in an action event handler, like [this post](https://blogs.oracle.com/jmxetc/entry/connecting_scenebuilder_edited_fxml_to) talks about – Ben Navetta Apr 13 '15 at 22:09
  • public void initialize() throws IOException{ String FILENAME = "au_studentlist.txt"; FileOutputStream outfile = new FileOutputStream(FILENAME, true); ObjectOutputStream outstream = new ObjectOutputStream(outfile); outstream.writeUTF("\n StudentID: " + txtStudentID.getText() + "\n First Name: " + txtFName.getText() + "\n Last Name: " + txtLName.getText() + "\n Major: " + cboMajor.getValue() + "\n State: " + cboState.getValue() + "\n Expected Graduation Date: " + dtExpGraduationDate.getValue().toString()); btnCancel.setOnAction((event) -> { System.exit(0); }); } – Kimberly Stickley Apr 13 '15 at 22:18
  • This is what I changed it to and I am getting a error and I do not know why. The error it is giving me is Null Pointer Exception on line 44 which is everything up until toString()); – Kimberly Stickley Apr 13 '15 at 22:19
  • That code will run as soon as the scene finishes loading, so the UI controls won't have meaningful values yet. `dtExpGraduationDate.getValue()` is probably returning `null`. – Ben Navetta Apr 13 '15 at 22:21
  • How am I supposed to type anything in the text fields to give it a value if keeps giving me an error? – Kimberly Stickley Apr 13 '15 at 22:30
  • Add a button that the user can press when they're done typing. Put the code to write to a file in an event handler attached to that button like with `btnCancel`. – Ben Navetta Apr 13 '15 at 22:32
  • I understand that but I cant even type in the text fields in order for anything to write to a file because when I go and run the program it doesnt load all the way because of the Null Pointer Exception Error. I have a Save Data Button and a Cancel Button on the GUI but none of that matters if I cant even get the program to fully run – Kimberly Stickley Apr 13 '15 at 22:39
  • You're getting a NullPointerException because your file code is running in the `initialize` method, not in the Save Data Button handler. If you keep that code where it is, it will always crash. – Ben Navetta Apr 13 '15 at 22:44
  • Then how do I do that? – Kimberly Stickley Apr 13 '15 at 22:50