Reputation: 41
Disclaimer: I've went over many examples and read the docs.
This question entails 3 classes. My serialized class, a helper class that handles I/O, and an activity for interfacing.
My serialized class looks something like this:
public class EntityPlayer implements Serializable {
private static final long serialVersionUID = -7671612522335708108L;
String name = "foo";
public EntityPlayer() {
name = "foo";
}
}
My helper class looks like this (this is where the heavy lifting is done):
public class Main {
static Object loadPlayer(Context c, String name) throws IOException,
ClassNotFoundException {
FileInputStream f = c.openFileInput(name);
ObjectInputStream i = new ObjectInputStream(f);
Object player = i.readObject();
i.close();
return player;
}
static void savePlayer(Context c, String name, EntityPlayer player) throws IOException {
FileOutputStream f = c.openFileOutput(name, Context.MODE_PRIVATE);
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(player);
o.flush();
o.close();
}
}
and then finally, this is how I'm accesing (in a nut shell):
public class DisplayInteract extends Activity implements OnClickListener {
EntityPlayer player = new EntityPlayer(); //by editing this line, my issue was fixed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.g_interact);
player = new EntityPlayer(ID, this);
try {
Main.savePlayer(player.name, player);
} catch (IOException e) {
}
try {
player = (EntityPlayer) Main.loadPlayer("Derp");
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}
}
}
Basically I'm getting a NPE when I make any calls concerning the serialized class after loading it. I know the issue is in the way that I'm saving the file because I can tell that there is no file through the Android file explorer in eclipse. I know the problem also is in how I'm loading the file because (I have no ideal how) I got it to work at one point (it created a file) but I still couldn't access any of the data without it throwing an NPE (because I wasn't accessing the class correctly)
Upvotes: 0
Views: 437
Reputation: 2018
You're not specifying a path where the file should be saved to.
Use the openFileOutput()
from Context which will save it to your applications data folder.
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
Upvotes: 2
Reputation: 3179
I don't know how you're generating a filename, but you might want to make sure you have storage available (free space/not in use by usb mass storage/etc.). Try something along the following lines:
//Is external storage available?
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state))
{
//If we can't get storage, let the user know
return;
}
//Create the actual file
//The file should end up in /mnt/sdcard/Android/data/your.package.name/files/
File dir = new File(Environment.getExternalStorageDirectory() + EXTERNAL_FILES_DIR_EXTENTION);
//Make all needed directories if they do not exist
dir.mkdirs();
//Create the file
File file = new File(dir, fileName);
Upvotes: 1