Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I used FileInputStream class when I was trying to read something from my files reside in SDCard, the codes are as follows: Code:

filepath = "/sdcard/myfile/testFile" FileInputStream fileIn = null;
fileIn = new FileInputStream(filepath); byte [] InBuf = new byte[1024];
fileIn.read(InBuf); Strubg fileContent = new String(InBuf);
fileIn.close();

when I ran my program, android keep reporting null pointer error on my last line:"fileIn.close", I really didn't know the reason

share|improve this question

2 Answers

Use it like this:

filePath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfile/textfile.extenstion"

You must be missing the extension.

share|improve this answer
Sorry, I didn't miss any permission. NULL pointer exception couldn't be caused by permission missing. – Terry Yoon Mar 15 '11 at 0:20
up vote 0 down vote accepted

I worked till late last night, finally, I make it work, though I am not sure if it is exact reason. I simply tried the following methods, then no more null pointer report came,

try  
{
    InputStream fileIn = null;
    filepath = "/sdcard/myfile/testFile";
    fileIn = new BufferedInputStrream(new
    FileInputStream(filepath));
    byte [] InBuf = new byte[1024];
    fileIn.read(InBuf);
    ...
    ...
    ...  
} catch(Exception e) {
    e.printStackTrace();  
} finally {     
    if(fileIn != null) 
    {       
        try         
        {           
            fileIn.close();         
        } catch(IOException e) {            
            e.printStackTrace();        
        }   
    } 
}

Any way, thank you so much for your help. ^^

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.