basically I saved an Object of the class Server, which works because I can read out names of the files I saved and put them in a spinner, but I can't seem to figure out how to load it back to be able to use it.
Following the code I used save it and try to load it
EditText serverName;
EditText textGetter;
String filename;
public void saveServer(View view) {
filename = serverName.getText().toString() + ".bin";
Server newServer = new Server();
textGetter = (EditText) findViewById(R.id.nameEdit);
newServer.setName(textGetter.getText().toString());
textGetter = (EditText) findViewById(R.id.serverEdit);
newServer.setAddress(textGetter.getText().toString());
textGetter = (EditText) findViewById(R.id.userEdit);
newServer.setUser(textGetter.getText().toString());
textGetter = (EditText) findViewById(R.id.passwordEdit);
newServer.setPassword(textGetter.getText().toString());
try {
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(filename));
os.writeObject(newServer);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadServer() {
String selectedServer = String.valueOf(serverSpinner.getSelectedItem());
selectedServer = selectedServer + ".bin";
Server loadedServer = null;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(selectedServer));
loadedServer = (Server) is.readObject();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//following Line is highlited by logcat at app.FtpActivity.loadServer(FtpActivity.java:69)
Toast.makeText(this, loadedServer.getName(), Toast.LENGTH_SHORT).show();
}
Logcat Error:
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: FATAL EXCEPTION: main
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: java.lang.NullPointerException
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at app.FtpActivity.loadServer(FtpActivity.java:69)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at app.FtpActivity$1.onItemSelected(FtpActivity.java:44)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.widget.AdapterView.fireOnSelected(AdapterView.java:895)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.widget.AdapterView.access$200(AdapterView.java:50)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4921)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
10-15 14:37:14.119 16083-16083/app E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
.
at app.FtpActivity$1.onItemSelected(FtpActivity.java:44)
refers to the line in an onClick that only does
loadServer();