I want to post my images to my facebook page. Below is the code I tried which is from RestFB manual page. But it's showing some kind of error which is not getting understood. Also in publish("me/photos") should I pass my username instead of writing me?. Also the image bird.jpg exists in my project folder. Can anyone help me? Any kind of help will be appreciated
FacebookClient facebookClient = null;
FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("bird.jpg", FaceBookUpload.class.getResourceAsStream("/bird.jpg")),
Parameter.with("message", "Test cat"));
System.out.println("Published photo ID: " + publishPhotoResponse.getId());
This is the error which I encountered
Exception in thread "main" java.lang.IllegalArgumentException: Binary attachment data cannot be null.
at com.restfb.BinaryAttachment.<init>(BinaryAttachment.java:68)
at com.restfb.BinaryAttachment.with(BinaryAttachment.java:113)
at pkg.am.ncrb.shot.FaceBookUpload.main(FaceBookUpload.java:31)
Also I tried the below code
But none of it is working
InputStream is = new FileInputStream(new File("bird.jpg"));
FacebookType publishVideoResponse =facebookClient.publish("me/photos",FacebookType.class,
BinaryAttachment.with("bird.jpg", is),
Parameter.with("message", "MY PHOTO POST"));
and it's getting exception
Exception in thread "main" java.lang.NullPointerException
at pkg.am.ncrb.shot.FaceBookUpload.main(FaceBookUpload.java:35)
I just need a sample piece of code that push my image into facebook. I don't know what went wrong.
In Java, you can't use an object that is set to null
. You'll get a NullPointerException
.
The object that is null
is the FacebookClient
. You should initialize it:
FacebookClient facebookClient = new DefaultFacebookClient();
Where are you putting the photo? If it's embedded in the jar file, use FaceBookUpload.class.getResourceAsStream("/bird.jpg")
. If it's in the current directory (probably), use new FileInputStream(new File("bird.jpg"))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With