Reputation: 179
I am developing an application and I have a problem when I try to save a file into the internal storage.
The file is an xml and it's on the res/raw folder on my project. When the application starts, it checks if the file exists or not. If it doesn't exist, it copies the file from the res/raw folder into the internal storage, else it retrieves a field inside the file to check its version and if it's a newer version, it deletes the previous file and copies it again.
Well, the problem is that if I have a new version and I have deleted the previous one, when I'm going to copy it again, it throws an NullPointerExceptcion and I can't find the problem, but I'm using the same code for both situations and I don't know how to solve it.
This is the code I'm using:
private void writeFileToInternalStorage() {
FileOutputStream fos = null;
try {
InputStream fXmlFile = getResources().openRawResource(R.raw.partidos);
File file = getBaseContext().getFileStreamPath("file.xml");
if(file.exists()) {
String resourcesVersion= getFileVersion(fXmlFile);
String localVersion = getFileVersion(new FileInputStream(file));
if (!resourcesVersion.equalsIgnoreCase(localVersion)) {
//file.delete();
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
else {
fos = openFileOutput("file.xml", Context.MODE_PRIVATE);
copyFile(fos, fXmlFile);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
protected void copyFile(OutputStream fos, InputStream fXmlFile) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = fXmlFile.read(buffer)) > 0){
fos.write(buffer, 0, bytesRead);
}
}
protected String getFileVersion(InputStream file) {
String version = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
version = doc.getElementsByTagName("numero").item(0).getFirstChild().getNodeValue();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return version;
}
Any tips for solving it??
PS: The exception is thrown on this statement:
while((bytesRead = fXmlFile.read(buffer)) > 0)
PS2: This is the code from LogCat:
03-03 20:47:07.140: W/System.err(2166): java.lang.NullPointerException: asset
03-03 20:47:07.148: W/System.err(2166): at android.content.res.AssetManager.readAsset(Native Method)
03-03 20:47:07.158: W/System.err(2166): at android.content.res.AssetManager.access$700(AssetManager.java:35)
03-03 20:47:07.168: W/System.err(2166): at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:573)
03-03 20:47:07.168: W/System.err(2166): at com.jfma75.myapp.MyApp.copyFile(MyApp.java:291)
03-03 20:47:07.178: W/System.err(2166): at com.jfma75.myapp.MyApp.writeFileToInternalStorage(MyApp.java:263)
03-03 20:47:07.178: W/System.err(2166): at com.jfma75.myapp.MyApp.onCreate(MyApp.java:62)
03-03 20:47:07.188: W/System.err(2166): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
03-03 20:47:07.188: W/System.err(2166): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
03-03 20:47:07.198: W/System.err(2166): at android.app.ActivityThread.access$1300(ActivityThread.java:130)
03-03 20:47:07.198: W/System.err(2166): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
03-03 20:47:07.208: W/System.err(2166): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 20:47:07.208: W/System.err(2166): at android.os.Looper.loop(Looper.java:137)
03-03 20:47:07.218: W/System.err(2166): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 20:47:07.218: W/System.err(2166): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 20:47:07.228: W/System.err(2166): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 20:47:07.237: W/System.err(2166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 20:47:07.237: W/System.err(2166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 20:47:07.248: W/System.err(2166): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 684
Reputation: 690
Just added my comment as the answer since it seemed to fix the OP's issue and will close this question:
I think I know what the problem is. You open the resource file and hold it in an
InputStream
then you callString resourcesVersion= getFileVersion(fXmlFile);
which, I guess, is your own private function to get the version. I'm betting you advance theInputStream
or close it in that function. Then, when you later callcopyFile
theInputStream
is either consumed or closed. Try just removing that line and put a constantString resourcesVersion = "versionThatDoesn'tMatch";
just to test theInputStream
Upvotes: 1
Reputation: 7533
It appears that in the statement
InputStream fXmlFile = getResources().openRawResource(R.raw.partidos);
the variable fXmlFile
is null
. You can check why that happens.
Upvotes: 0