1

My app is meant to allow users to upload a video to their Facebook wall. The following code is very similar to many other examples of working code This code gives me the null pointer exception:

Here is my main class:

public class MainActivity extends Activity {

Session session;
Facebook facebook=new Facebook("367714506687419");
private int mAuthAttempts = 0;
String access_token=null;
long expires=0;
Context context;
String Permissions[]=new String[] {
        "publish_stream", "read_stream" , "video_upload" , "publish_actions" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button logout=(Button) findViewById(R.id.logout);
    context=this;
    Button upload_video=(Button) findViewById(R.id.upload_video); 
    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {


      @Override
      public void call( Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @SuppressWarnings("deprecation")
            @Override
            public void onCompleted(GraphUser user, Response response) {



              if (user != null) {

                  TextView welcome = (TextView) findViewById(R.id.welcome);
                welcome.setText("Hello " + user.getName() + "!");


                if (access_token != null) {
                    facebook.setAccessToken(access_token);
                }

                if (expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                 if (!facebook.isSessionValid()) {

                        facebook.authorize(MainActivity.this, Permissions,              new DialogListener(){

                            @Override
                            public void onComplete(Bundle values) {

                                access_token=facebook.getAccessToken();
                                expires=facebook.getAccessExpires();

                            }

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

                            }

                            @Override
                            public void onError(DialogError e) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onCancel() {
                                // TODO Auto-generated method stub

                            }

                        });

                    }

              }
            }
          });
        }
      }
    });






    upload_video.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View arg0) {
            if(session.isOpened()){
            //String dataPath = "/mnt/sdcard/DCIM/Camera/VID_20130725_023450.mp4";
            String path="/sdcard/DCIM/Camera/1.mp4";
            byte[] data = null;
            String dataPath = "/sdcard/DCIM/Camera/1.mp4";
            String dataMsg = "Your video description here.";
            String dataName = "1.mp4";
            Bundle param;
            facebook = new Facebook("367714506687419");
            AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
            InputStream is = null;
            try {
                is = new FileInputStream(dataPath);
                data = readBytes(is);
                param = new Bundle();
                param.putString(Facebook.TOKEN,  access_token);
                param.putString("message", dataMsg);
                param.putByteArray("video", data);
                //param.putString("filename", dataName);
                mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
            }
            catch (FileNotFoundException e) {
               e.printStackTrace();
            }
            catch (IOException e) {
               e.printStackTrace();
            }

            }
            else{
                Log.d("Please Check ur access tocken", "");
            }
        }
    });

And Here is fbRequestListener.java

public class fbRequestListener implements RequestListener {

@Override
public void onComplete(String response, Object state) {
    Log.e("response", response);

}

@Override
public void onIOException(IOException e, Object state) {
    Log.e("", "onIOException");
    e.printStackTrace();
}

@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
     Log.e("", "onFileNotFoundException");
        e.printStackTrace();

}

@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
     Log.e("", "onMalformedURLException");
        e.printStackTrace();

}

@Override
public void onFacebookError(FacebookError e, Object state) {
    Log.e("", "onFacebookError");
    e.printStackTrace();

}

}

Log Cat is :

FATAL EXCEPTION: Thread-1044 java.lang.NullPointerException

at libcore.net.UriCodec.decode(UriCodec.java:149)

at java.net.URLDecoder.decode(URLDecoder.java:45)

at com.facebook.android.Util.openUrl(Util.java:186)

at com.facebook.android.Facebook.requestImpl(Facebook.java:806)

at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:**

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Hafiz.M.Usman
  • 223
  • 3
  • 21

2 Answers2

0

You appear to be trying to directly push the video into the graph API (graph.facebook.com) whereas it needs to go to graph-video.facebook.com as a multipart/formdata

There's a good explanation of how this works at https://developers.facebook.com/blog/post/493/ and you can find out more about uploading as multipart/formdata from Java at How can I make a multipart/form-data POST request using Java?

Community
  • 1
  • 1
Reuben Thompson
  • 351
  • 5
  • 10
0

You are mixing two different paradigms in your code. Please use only the Session class and NOT the Facebook class (since it's been deprecated).

Also, since you're using the 3.0 version of the SDK, there's a helper method that uploads videos for you. See the docs here:

https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadVideoRequest(Session,%20File,%20Callback)

Ming Li
  • 15,672
  • 3
  • 37
  • 35