I just released an Android app that parses a local file and processes some data. A few days ago, one of my clients informed me of an error, every time he tries to process his file, the application crashes.
This is the error log he sent me:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: lock == null
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:120)
And the code associated with this is as follows:
public void selectFile()
{
List<File> files = getDocsToParse();
this.listview.setAdapter(this.myadapter);
this.listview.setOnItemClickListener(new OnItemClickListener()
{
...
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
parseFile(files.get(position));
}
...
}
this.myadapter.addFiles(files);
}
public static List<File> getDocsToParse() {
File sdcard = Environment.getExternalStorageDirectory();
File subdir = new File(sdcard, "MyAppFolder/Files/");
if (!subdir.exists()) {
subdir.mkdirs();
}
File files[] = subdir.listFiles();
List<File> filterFiles = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
File file = files[i];
filterFiles.add(file);
}
return filterFiles;
}
public void parseFile(File fileToParse)
{
long totalSize = 0;
InputStream is = null;
try {
totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
try {
while ((line = reader.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
}
The line that fails is as follows:
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
I understand that this is because "is" is null, and I will have to catch this case to avoid the application crashing (I will fix this in my next version).
EDIT 3: you are right, greenapps, I will check if the value is null before using it, otherwise I will not use it.
, "is" null, IOException :
totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));
EDIT 4: , IOEXception, , .
, 2 IOException.
, fileToParse , , , "" , files.get(i).getName(), .
, , , .
, . , ?
!
5: ctarabusi, parseFile :
public void parseFile(File fileToParse)
{
long totalSize = 0;
InputStream is = null;
try {
totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
while ((line = reader.readLine()) != null) {
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error parsing file", Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if(is != null)
{
try
{
is.close();
}
catch (IOException e)
{
Log.e("", e.getMessage(), e);
}
}
}
}
, , " ", .
?