Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

NullPointer Exception while getting input stream from fileconnection

Hi,

 

I have 9 images on the SD card which i have placed on "file:///SDCard/BlackBerry/image/". Now i am able to get the number of images on this image folder. But when i am converting the file to a bitmap image, after 7th image i am getting Null Pointer Exception. But instead of 9 images, when using 6 images, it is working correctly. Here is my code:

 

 public Bitmap convertBitmap(String path)
    {
    	Bitmap bmp = null;
    	InputStream input = null;
    	try {
    		
			FileConnection fconn = (FileConnection)Connector.open(path, Connector.READ);
			
			if (fconn.exists()) {
				Logger.out("ConvertBitmap", "before getting open input stream");
				try
				{
					input = fconn.openInputStream();
				}
				catch (Exception e) {
					Logger.out("ConvertBitmap", "exception**********   "+e.getMessage());
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				byte[] data = new byte[(int) fconn.fileSize()];
				input.read(data);
				bmp = Bitmap.createBitmapFromPNG(data, 0, data.length);
			
			}
		} catch (Exception e) {
			Logger.out("ConvertBitmap", "exception   "+e.getMessage());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bmp;
    	
    }

 This line basically giving the issue:

input = fconn.openInputStream();

 

Can anyone help what i am doing wrong here. 

 

Thanks, 

Arindam.

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: NullPointer Exception while getting input stream from fileconnection

you seem to be missing some cleanup code. connections have to be closed after being used, for example in a finally block.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: NullPointer Exception while getting input stream from fileconnection

So you mean i have to add this part of code:

 

 finally 
            {
                
                if (fc != null) 
                {   
                    // Everything is read, make sure to close the connection
                    try 
                    {
                        fc.close();
                        fc = null;
                    } 
                    catch (Exception ioex) 
                    {
                        Dialog.alert("exception threw " + ioex.toString());        
                    }
                }
            }

 Hope then everything will be fine. Right?

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: NullPointer Exception while getting input stream from fileconnection

i would also suggest that you execute blocking operations, like file i/o, on a separate thread.
see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
arindamhit
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10

Re: NullPointer Exception while getting input stream from fileconnection

Sure..If that is not going to fix my issue, i will try that also. Sure. 

Thanks.


simon_hain wrote:
i would also suggest that you execute blocking operations, like file i/o, on a separate thread.
see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

 

Please use plain text.