Reputation: 3
I'm using Eclipse to work on a college assignment, still learning how to code so please ignore the shoddy work in the attached file! I've created a class for filehandling and the strangest thing is happening, Eclipse is telling me that a semicolon is expected where one already exists. I can't see what the problem is at all, commented out the rest of the class to see if that made any difference but the problem still exists.
The error appears on the line ObjectOutputStream superObjectOutputStream;
.
Here is my code:
package filehandling;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class SuperstarFile {
File superFile;
FileInputStream superFileInputStream;
FileOutputStream superFileOutputStream;
ObjectInputStream superObjectInputStream;
ObjectOutputStream superObjectOutputStream;
superFile = new File("superstars.data");
/*
public void saveFile (ArrayList maleWrestler)
{
try
{
superFileOutputStream = new FileOutputStream(superFile);
superObjectOutputStream = new ObjectOutputStream(superFileOutputStream);
superObjectOutputStream.writeObject(maleWrestler);
superObjectOutputStream.close();
superFileOutputStream.close();
}
catch(final Exception e){
JOptionPane.showMessageDialog(null, "Ooops - " +e);
}
}
public ArrayList readFile(ArrayList goingIn)
{
ArrayList temp;
try{
superFileInputStream = new FileInputStream(superFile);
superObjectInputStream = new ObjectInputStream(superFileInputStream);
temp = (ArrayList) superObjectInputStream.readObject();
superObjectInputStream.close();
superFileInputStream.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Ooops - " +e);
}
return temp;
}
*/
}
Upvotes: 0
Views: 1044
Reputation: 48404
You are assigning a value to your instance field without curly brackets or the body of a method or constructor surrounding your assignment.
Change:
superFile = new File("superstars.data");
To:
{
superFile = new File("superstars.data");
}
... for an instance block.
Otherwise put the assignment in a constructor:
SuperstarFile() {
superFile = new File("superstars.data");
}
You can also use an instance method:
void doSomething() {
superFile = new File("superstars.data");
}
Or, as stated elsewhere, you can assign your field directly inline with the declaration:
File superFile = new File("superstars.data");
Upvotes: 3
Reputation: 3296
Your problem isn't with ';' but with assignment that goes after it. Change code like this:
public class SuperstarFile {
FileInputStream superFileInputStream;
FileOutputStream superFileOutputStream;
ObjectInputStream superObjectInputStream;
ObjectOutputStream superObjectOutputStream;
File superFile = new File("superstars.data");
...
}
And it will not give error message.
But to make some real job your file should have methods.
Upvotes: 1