Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Retrieving and Viewing Messages Viewing Video Messages

Slava Fleer
Slava Fleer
6,086 Points

when I choose to see text, app crushes, before entering to relevant code.

HI. I also did text type of message. so I did switch for 3 options.

   @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ParseObject message = mMessages.get(position);
        String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);
        ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);
        String messageText = message.getString(ParseConstants.KEY_MESSAGE);
        Uri fileUri = Uri.parse(file.getUrl());

        switch (messageType) {
            case ParseConstants.TYPE_TEXT:
                // View Text Message
                //Toast.makeText(getActivity(), messageText, Toast.LENGTH_LONG).show();
                break;
            case ParseConstants.TYPE_IMAGE:
                // View Image
                Intent imageIntent = new Intent(getActivity(), ViewImageActivity.class);
                imageIntent.setData(fileUri);
                startActivity(imageIntent);
                break;
            case ParseConstants.TYPE_VIDEO:
                // View Video
                Intent videoIntent = new Intent(Intent.ACTION_VIEW, fileUri);
                videoIntent.setDataAndType(fileUri, "video/*");
                startActivity(videoIntent);
                break;
        }
    }

Video and image are working as in Ben's videos, BUT when I am choosing the text, the app crushes. =(

the log doesn't say me much, may be someone would find the problem? thanks a lot.

04-19 16:02:23.593  17082-17082/com.slava.ribbit W/dalvikvm threadid=1: thread exiting with uncaught exception (group=0x41bd3c08)
04-19 16:02:23.598  17082-17082/com.slava.ribbit E/AndroidRuntime FATAL EXCEPTION: main
    Process: com.slava.ribbit, PID: 17082
    java.lang.NullPointerException
            at com.slava.ribbit.InboxFragment.onListItemClick(InboxFragment.java:76)
            at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
            at android.widget.AdapterView.performItemClick(AdapterView.java:308)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1509)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3467)
            at android.widget.AbsListView$3.run(AbsListView.java:4830)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

line 76 is

       Uri fileUri = Uri.parse(file.getUrl());

2 Answers

Hello,

It looks like the problem is that when you're doing Images and Videos, you are using a file. However, when you are sending just text, there is no stored file. If you were to move the lines

ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);

and

Uri fileUri = Uri.parse(file.getUrl());

into the case statements that deal with images and videos(I know, this breaks DRY for the moment). You should be able to not get the exception. I'd also recommend moving the message text line into the text case statement to be safe. I haven't gotten to test this out, but i believe it should help. Let me know if it does not and I will see if I can help further.

Slava Fleer
Slava Fleer
6,086 Points

yes. you are right. when i commented video and image cases as Parsefile and Uri, I could read the text . can you explain why it happening? thanks a lot.

Slava Fleer
Slava Fleer
6,086 Points

my new switch statement

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ParseObject message = mMessages.get(position);
        String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);


        switch (messageType) {
            case ParseConstants.TYPE_TEXT:
                String messageText = message.getString(ParseConstants.KEY_MESSAGE);
                // View Text Message
                Toast.makeText(getActivity(), messageText, Toast.LENGTH_LONG).show();
                break;
            case ParseConstants.TYPE_IMAGE:
                ParseFile imageFile = message.getParseFile(ParseConstants.KEY_FILE);
                Uri imageFileUri = Uri.parse(imageFile.getUrl());

                // View Image
                Intent imageIntent = new Intent(getActivity(), ViewImageActivity.class);
                imageIntent.setData(imageFileUri);
                startActivity(imageIntent);
                break;
            case ParseConstants.TYPE_VIDEO:
                ParseFile videoFile = message.getParseFile(ParseConstants.KEY_FILE);
                Uri videoFileUri = Uri.parse(videoFile.getUrl());
                // View Video
                Intent videoIntent = new Intent(Intent.ACTION_VIEW, videoFileUri);
                videoIntent.setDataAndType(videoFileUri, "video/*");
                startActivity(videoIntent);
                break;
        }

now it's working. thanks again. but still don't understand why =).

as i understand it must to use the file if we call it ?

The reason it was crashing before was that you were referencing a file in all situations even when you didn't have one included (the case of just a text message). So getParseFile "works" by returning null since there was nothing to return since no file was included. So when you call videoFile.getUrl(), you are calling the getUrl method on a null object, thus getting the NullPointerException.

Hello,

Without the rest of your code, I can only guess that when you call

 Uri fileUri = Uri.parse(file.getUrl());

that file is null. This can happen if

ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);

cannot find a ParseFile object with the given key. You might want to check where the ParseFile is added and make sure its the same key and that you're actually adding a ParseFile with the key.

Slava Fleer
Slava Fleer
6,086 Points

Thanks for checking. But why it works ok for 2 other cases? I don't do anything else before switch.

and here all of Ribbit code

https://github.com/slavafleer/Ribbit.git