0

I have some picture paths stored in a datastore and i am trying to convert them into drawables and display the in my image view, for some reason im getting a null pointer exception. Can someone please help me? Thanks

String pathName = selectedPlayer.getPicture(); Toast.makeText(this, pathName, Toast.LENGTH_SHORT).show(); Drawable d = Drawable.createFromPath(pathName); imageView.setImageDrawable(d); 
7
  • 1
    What does your logcat prints? Commented Sep 7, 2012 at 8:50
  • java.lang.nullpointerexception Commented Sep 7, 2012 at 8:53
  • what are the values in pathName ? Is it local image path(SDCARD) or web url ? Commented Sep 7, 2012 at 8:53
  • 1
    logcat usually also prints line number at which the exception occured Commented Sep 7, 2012 at 8:55
  • localimage.. Images are stored on the emulators gallery and the paths are stored in a datastore. Commented Sep 7, 2012 at 8:55

2 Answers 2

2

You must check file name is not null

then check that first if file exists or not

if(pathName!=null && pathName!="") <--CHECK FILENAME IS NOT NULL { File f = new File(pathName); if(f.exists()) <-- CHECK FILE EXISTS OR NOT { Drawable d = Drawable.createFromPath(pathName); imageView.setImageDrawable(d); } } 

EDIT : You have to initialize your imageview first like

imageView=(ImageView)findViewById(R.id.yourimageviewid); 
Sign up to request clarification or add additional context in comments.

3 Comments

hmm now the null pointer exception is here: imageView.setImageDrawable(d);
@NiraliPatel Have you initialized imageView using findViewById() ?
imageView=(ImageView)findViewById(R.id.yourimageviewid);
1

Look at imageView it should not be null.

And after that try this :

bm = null; try { bm = BitmapFactory.decodeFile(imageView); } catch (OutOfMemoryError e) { e.printStackTrace(); } imageView.setImageBitmap(bm); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.