Reputation: 145
What makes me crazy is that my program stops in the middle of try block and continuous after all catch blocks! Here are details. I got AsyncTask
public class BigBitmapLoader extends AsyncTask<Uri, Void, Bitmap>
{
public BigBitmapLoader(ScribblesView scribbles)
{
scribblesRef = new WeakReference<ScribblesView>(scribbles);
}
@Override
protected Bitmap doInBackground(Uri... params)
{
InputStream is;
try
{
ScribblesView scribs = scribblesRef.get();
if (scribs != null)
{
is = scribs.getContext().getContentResolver().openInputStream(params[0]);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap;
}
}
catch(FileNotFoundException e)
{
Log.e(ERROR_TAG, e.toString());
}
catch(IOException e)
{
Log.e(ERROR_TAG, e.toString());
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
ScribblesView scribs = scribblesRef.get();
if (scribs != null) scribs.setBigBitmap(bitmap);
}
private WeakReference<ScribblesView> scribblesRef;
private static final String ERROR_TAG = "BigBitmapLoader";
}
In doInBackground() it comes to is.close()
and then immediately jumps to return null
after all catch blocks. Thus it skips return bitmap
. At this point I got no exceptions. Only later when returned bitmap is used I got NPE. Any ideas?
Upvotes: 0
Views: 142
Reputation: 145
The reason was that I had an exception on main thread. But there was no links on it in log. Here if (scribs != null) scribs.setBigBitmap(bitmap);
in setBigBitmap()
I used uninitialized variable. Fixed it and all works. But debuger still "jumps". Maybe some bugs in eclipse's debuger, because now it returns a returns a proper value. And did it before. It was only uninitialized variable.
Thank you all for your answers)
Upvotes: 0
Reputation: 40256
It's failing because of a NullPointerException
from is
you just don't see it. When an exception occurs in an ExecutorService
from a Callable
or Runnable
the exception is swallowed (unless a UncaughtExceptionHandler is set). Note AsyncTask uses (or at least last I checked) the ExecutorService for async execution.
doInBackground
will run on another thread, if a RuntimeException occurs it will not print anywhere that isn't specified (ie swallow the exception).
I suggest you add a third catch block
} catch(RuntimeException ex){
ex.printStackTrace(); //or log
}
In short, the InputStream is probably null.
Upvotes: 2
Reputation: 5995
Well, the debugger's line numbers are sometimes off, so maybe that's the issue there. Do a clean build. Also, I would move is.close() to finally block. It's a good idea in general in order to make sure that you properly dispose the resources. So it would go something like this:
InputStream is = null;
try
{
// do stuff
} catch(FileNotFoundException e)
{
Log.e(ERROR_TAG, e.toString());
} catch(IOException e) {
Log.e(ERROR_TAG, e.toString());
} finally {
if (is != null) {
is.close();
}
}
Upvotes: 2
Reputation: 5637
You see no exceptions because, no exceptions occured
ScribblesView scribs = scribblesRef.get();
if (scribs != null)
{
is = scribs.getContext().getContentResolver().openInputStream(params[0]);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap; // return statement
}
The return statement maybe returns null. try debugging the method "decodeStream"
Upvotes: 1