1
public String[] get_file_names_from_SD() /* Compiler complains here */ 
{
    String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
        File root = Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/journal_storage");
        if (dir.exists() & dir.isDirectory()) {
            return (dir.list());            
        }

    } else {
        Toast.makeText(getActivity(), "Media Mounted Issue", Toast.LENGTH_SHORT).show();
    }
}

So I am returning dir.list() which will contain an array of strings but for some reason compiler is showing an error "This method must return a result of type String[]".

According to the Android documentation, the method returns an array of Strings.

Can anyone can tell me what I am doing wrong?

1
  • 2
    instead of &, there should be && in condition.. :) Commented Jul 25, 2013 at 5:47

4 Answers 4

2

All code paths of your method are NOT returning the String[]. You should return the String[] or null in the else block also since else block is a reachable code block of your method.

public String[] get_file_names_from_SD() /*Compiler complains here*/
    {
        String state = Environment.getExternalStorageState();
        if(state.equals(Environment.MEDIA_MOUNTED))
        {
            File root = Environment.getExternalStorageDirectory();
            File dir = new File(root.getAbsolutePath()+"/journal_storage");
            if(dir.exists() & dir.isDirectory())
            {
                return(dir.list());

            }

        }
        else
        {
        Toast.makeText(getActivity(), "Media Mounted Issue", Toast.LENGTH_SHORT).show();
        //--> return null;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Puıt this line after your else statement:

return null;

This will guarantee that your method will return successfully. However, you should watch for NullPointerExceptions where you invoke this method.

Comments

1

Not all the call paths lead to returning a String[], for e.g. if your SD card is not MOUNTED the control goes to the else block and doesn't return anything. You can simply end it with a return null; statement at the end of the block to make that error go away.

Comments

1

Your else block is not returning anything .. Thats Y the error is thrown

else
    {
    Toast.makeText(getActivity(), "Media Mounted Issue", Toast.LENGTH_SHORT).show();
    // return something here .. If nothing , return null
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.