0

My app is meant to allow users to take a picture and then upload it to their Facebook wall. The following code is very similar to many other examples of working code given on other SO questions pertaining to this exact same problem, yet still gives me the null pointer exception:

    private void postOnWall()
    {
        final String response = "";
    try
    {
        Bundle params = new Bundle();
        params.putString("message", getMessage());
        params.putByteArray("picture", byteArray);
        mAsyncRunner.request(me/feed, params, "POST", new SampleUploadListener(),
        null);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }       
}

I would simply like to get the image upload working before I can move on to adding the message with it. Any ideas?

EDIT: Here is the SampleUploadListener() that goes with the AsyncRunner(...) line:

public class SampleUploadListener extends BaseRequestListener {
    public void onComplete(final String response, final Object state) {
        try {
            // process the response here: (executed in background thread)
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String src = json.getString("src");

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."

        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }
}

LogCat:

06-22 13:23:45.136: W/System.err(20667): java.lang.NullPointerException
06-22 13:23:45.136: W/System.err(20667):    at     com.uncc.cci.TrashPickup.TrashPickupActivity.postwall(TrashPickupActivity.java:475)
06-22 13:23:45.136: W/System.err(20667):    at com.uncc.cci.TrashPickup.TrashPickupActivity$5$1.onClick(TrashPickupActivity.java:230)
06-22 13:23:45.140: W/System.err(20667):    at android.view.View.performClick(View.java:3511)
06-22 13:23:45.140: W/System.err(20667):    at android.view.View$PerformClick.run(View.java:14105)
06-22 13:23:45.140: W/System.err(20667):    at android.os.Handler.handleCallback(Handler.java:605)
06-22 13:23:45.140: W/System.err(20667):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-22 13:23:45.140: W/System.err(20667):    at android.os.Looper.loop(Looper.java:137)
06-22 13:23:45.140: W/System.err(20667):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-22 13:23:45.144: W/System.err(20667):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:23:45.144: W/System.err(20667):    at java.lang.reflect.Method.invoke(Method.java:511)
06-22 13:23:45.144: W/System.err(20667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-22 13:23:45.144: W/System.err(20667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-22 13:23:45.148: W/System.err(20667):    at dalvik.system.NativeStart.main(Native Method)
David B
  • 147
  • 1
  • 11
  • Do you have a picture to be loaded? `NullPointerException` is usually thrown when an `Object` is called while it is `null` when it should not be `null`. – Jason L Jun 21 '12 at 17:43
  • Which line (consult LogCat or post it)? – Michał Klimczak Jun 21 '12 at 17:45
  • @Yawus: 'byteArray' is a byte array from a bitmap that I've already handled. I checked it to see if it was null and it wasn't. – David B Jun 21 '12 at 18:02
  • @MichałK: the mAsyncRunner.request(...) line gives the null pointer exception – David B Jun 21 '12 at 18:02
  • Well, `params`, `"POST"` and `new SampleUploadListener()` are surely not null. It must be one of these two `null`s which gives you NPE. Just check what should go there (I don't know what kind of object is your `mAsyncRunner`) and post it instead of `null`. – Michał Klimczak Jun 21 '12 at 18:08
  • As was suggested in the answer below, I changed the first null to "me/photos", but am still receiving the same error. The mAsyncRunner is of type AsyncFacebookRunner. – David B Jun 21 '12 at 18:48
  • you can upload a byteArray to me/photos, but you can not upload a bytearray as a post to a wall. To post an image to a wall you HAVE to provide a valid URL for where that image already resides on the internet. – Yevgeny Simkin Jun 22 '12 at 20:31

2 Answers2

0

Should be:

private void postOnWall() {
    Bundle params = new Bundle();            
    params.putString("message", "New picture");
    params.putByteArray("source", byteArray);
    mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}

You sent the path as null and that's probably why you got the null pointer exception.
Also according to the documentation the image parameter name is source.


Edit

Try this to check if mAsyncRunner is null:

private void postOnWall() {
    Bundle params = new Bundle();            
    params.putString("message", "New picture");
    params.putByteArray("source", byteArray);
    Log.d("Facebook-Example", mAsyncRunner == null ? "mAsyncRunner is null" : "mAsyncRunner not null");
    mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}

Since the error is thrown in that line, it seems like the only possible thing.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • I changed my code to reflect your advice, but unfortunately I still am getting the same null pointer exception on the same line. Is there anything that I should be including in my SampleUploadListener() that may cause this? – David B Jun 21 '12 at 18:44
  • Can you post your modified code and the code for SampleUploadListener? – Nitzan Tomer Jun 22 '12 at 09:28
  • I've updated my question to reflect the changes, uploadListener, and logcat – David B Jun 22 '12 at 17:26
  • Are you sure that the `mAsyncRunner` has been constructed? – Nitzan Tomer Jun 22 '12 at 18:49
  • Yes, it is of AsyncFacebookRunner type (from com_facebook_android). I just checked back through the documentation for this, and found this: "Make a request to the Facebook Graph API with the given HTTP method and string parameters. Note that binary data parameters (e.g. pictures) are not yet supported by this helper function." I've seen many other posts here on SO that have used this same method (with the HTTP "POST" argument) that claim success in posting a byte array image, so this is confusing to me. – David B Jun 22 '12 at 19:07
  • That refers to the `request` method of the regular `Facebook` object, not `AsyncFacebookRunner`. I edited my answer. – Nitzan Tomer Jun 22 '12 at 20:21
0

When you have corrected whatever bug you have here, you will unfortunately discover that it is not possible at this time to post an image to your wall by uploading the actual image bytes directly to facebook. You can upload a photo to their photos gallery, but not to their wall. To post a photo to the wall it must already reside online and you will need to provide a link to where it lives along with the other data. If I'm mistaken then this is a very new development as I just encountered all this a few months ago and even filed a bug with Facebook which they rejected saying that "it's not a bug, it's how we intended things to be".

If you're ok with putting the photo in their gallery (if it's the only photo IN the gallery it will look almost exactly like a regular wall post) this is the way to do it: Android post picture to Facebook wall

Community
  • 1
  • 1
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • 1
    I had come to that conclusion after reading more carefully through their documentation, but was hoping that they had released something more appropriate in the mean time. Unfortunately uploading a photo to their gallery isn't a good plan for the app, so I'll find another work-around. Thank you for your answer confirming my fears! – David B Jun 24 '12 at 04:54