NullReferenceException while calling Twitter.Compose() in Unity

unity

#1

Hi, there

I’m trying Fabric for Unity, and I’ve followed the documents to install and setup Fabric & Twitter Kit.
I was testing my App on iOS 10 device with Twitter App installed.

Twiiter.Login() API works well, and I’ve got a valid session too.
But when I want to try Twitter.Compose() API, Unity gave me an Exception below,

NullReferenceException: A null value was found where an object instance was required.
at Fabric.Internal.Twitter.IOSTwitterImpl.Compose (Fabric.Twitter.TwitterSession session, Fabric.Twitter.Card card, System.String[] hashtags) [0x00000] in :0

I’ve checked three args, session is valid, card is constructed by the demo script like this, and hashtags=null by default.

    public static void startComposer(TwitterSession session, String imageUri)
    {
        Card card = new AppCardBuilder()
          .ImageUri(imageUri)
          .GooglePlayId("com.company.example")
          .IPhoneId("123456789")
          .IPadId("123456789");
        Twitter.Compose(session, card);
    }

I’ve search for similar situation inside the community and google, but in vain.
Hope I could get some help by post my question.
Thank you.


#2

Hey @tcdnw,

Could you share the full Console output when this happens including information before and after the error?

-Mike


#3

Hi @bonnell
Stacktrace before Fabric.Internal.Twitter.IOSTwitterImpl.Compose is my own logic. Like this

NullReferenceException: A null value was found where an object instance was required.
  at Fabric.Internal.Twitter.IOSTwitterImpl.Compose (Fabric.Twitter.TwitterSession session, Fabric.Twitter.Card card, System.String[] hashtags) [0x00000] in <filename unknown>:0 
  at MoleMole.TwitterShare.startComposer (Fabric.Twitter.TwitterSession session, System.String imageUri) [0x00000] in <filename unknown>:0 
  at MoleMole.TwitterShare.LoginComplete (Fabric.Twitter.TwitterSession session) [0x00000] in <filename unknown>:0 
  at MoleMole.BasePlatform.SharePhoto (System.String text, System.String path) [0x00000] in <filename unknown>:0 
  at MoleMole.ShareManager.SharePhoto (Int32 index) [0x00000] in <filename unknown>:0 
  at MoleMole.SimpleRectButtonBase.Core () [0x00000] in <filename unknown>:0 
  at MoleMole.MonoSharePhotoDialog.Update () [0x00000] in <filename unknown>:0 
 
(Filename: currently not available on il2cpp Line: -1)

Before calling Twitter.Compose()
I have a debug log about session and card.

Card info=promo_image_app|_iphoneid|ipadid|googleplayid_|/var/mobile/Containers/Data/Application/FAD20A83-3348-4654-9D2F-CDF98667174E/Documents/Pic.png

session=Fabric.Twitter.AuthToken|_mytwitterid|mytwitteraccount_

My exception caught logic ends my app after the NullRef, so no more console log after that.


#4

Thanks @tcdnw, based on that log, it sounds like the Filename being passed in isn’t valid. Can you try doing a check on the device that the file itself is still present at the time you’re trying to compose the tweet. Perhaps a reference is being lost from the time the screenshot is taken to sharing?


#5

Thank you for your reply, @bonnell
I found a jailbreak device and I can reach the img in file explorer while my app finishing capture screenshot
I thought that the absolute img path is valid, because I will give user a preview of screenshot in my own app, using the same path, before user press the tweet button.

So, maybe there are other problems we haven’t taken in to consider.
What exactly Card.ImageUri want?

/var/mobile/Containers/Data/Application/FAD20A83-3348-4654-9D2F-CDF98667174E/Documents/Pic.png

String like that or in other formation?

Another question, I have tried Twitter.Compose() on android device, this time I have got another error msg.

AndroidJavaException: java.lang.IllegalStateException: Must Initialize Fabric before using singleton()
java.lang.IllegalStateException: Must Initialize Fabric before using singleton()
at io.fabric.sdk.android.Fabric.singleton(Fabric.java:273)
at io.fabric.sdk.android.Fabric.getKit(Fabric.java:524)
at com.twitter.sdk.android.core.TwitterCore.checkInitialized(TwitterCore.java:159)
at com.twitter.sdk.android.core.TwitterCore.getInstance(TwitterCore.java:76)
at com.twitter.sdk.android.unity.TwitterKit.session(TwitterKit.java:68)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.a(Unknown Source)
at com.unity3d.player.UnityPlayer$b.run(Unknown Source)

(Filename: currently not available on il2cpp Line: -1)

I’ve tried to configure the INIT automatic from Fabric/Advanced/Manual Init Menu in Unity, but nothing changed.
When I want to INIT manually, the instruction of add Fabric.Initialize(); maybe have sth wrong?
There wasn’t any function fits.

I’ve read your other reply about this, but I cannot call Fabric.Runtime.Fabric.Initialize() either.
There wasn’t any function fits too.

I only chose Twitter v2.0.0 while Prepare Fabric.

Now I have two questions, one about Fabric for Unity on iOS, another about Fabric for Unity on Android.
BTW, I worked in GMT+08:00, I can’t just reply in time, thank you for your patience :slight_smile:


#6

Thanks @tcdnw for the additional details.

For Android - do you any references to Fabric before the init - any custom logs or keys being set?

On iOS - the URI shouldn’t have any file prefixes like this: file:// Just wanted to confirm you don’t have that set - though based on the log it doesn’t look like it.

-Mike


#7

Hi @tcdnw,

Here are some steps you can take to debug on the Android side. Our plugin states that the TwitterKit GameObject needs to be dragged into the first scene. Make sure it is dragged there.

What is the first scene? To determine the first scene, open “Build Settings” and looking at the top-most item in the “Scenes in Build” list. Hope that helps.

-Vamsi


#8

Hi, @bonnell

For Android
I only set Twitter Key&Secret through Unity Menu Fabric/Prepare Fabric
There is a new iOS app appeared under my Fabric account when I launch it first time.

My Twitter Key was from apps.twitter.com, not the key generated in Fabric.
Before my iOS app first launch, I didn’t aware about Fabric Key

And I had received a e-mail that informed me my Twitter Key was whiltelisted

When Unity start build my android project, Twitter Kit edit AndroidMenifest.xml automatically.
But I didn’t see a new Android app appear under my Fabric account after launch.

For iOS
Absolute path correct, png file valid, so what makes the NullRefException?
Can I get an specific log or stacktrace about which parameter goes wrong?

@vam_si Nice to have you join our discuss.
In Fabric Prepare, it do reminds me that I should drag GameObject in my first scene.
FabricInit.cs & TwitterInit.cs
I added them in my first scene both iOS and Android project.

There is a new iOS app appeared under my Fabric account when I launch it first time.
I think it means Fabric Init successfully in iOS?

I’ve checked two .cs files generated by Fabric/Twitter Kit.

namespace Fabric.Internal
{
using UnityEngine;
using System.Collections;

public class FabricInit : MonoBehaviour
{
}
}

Actually it is empty?

Further more, How can I try to Init Fabric manually?


#9

Hello everyone,
I had the same problem on iOS (the NullReferenceException).

After checking that both parameters (the URI and the Twitter session) were valid, I tried passing a non-null value in the hashtags parameter, and it worked.
My tests so far:
Twitter.Compose(session, card, new string[] { "JustSomeText" });
Twitter.Compose(session, card, new string[] { "" });
In both cases: No NullReferenceException and no hashtag shows up.

Twitter.Compose(session, card, new string[] { "#ThisIsAProperHashtag" });
No NullReferenceException and the hashtag shows up with the App Card composer.

I’ll make an educated guess and say this is probably a bug in the iOS implementation.

I’m still unable to post App Cards on iOS because the project I’m working on is not yet published in the App Store.

I had no problem in the Android build of my project, I’m calling compose without passing any hashtag parameter and App Cards work just fine.

Hope this helps.


#10

@andreswx Thank you for your help, bro,
I have an app online,
I am going to try your method, if it works I will come back and mention it.


#11

@andreswx
It really make progress, When I gave an additional param of hashtag, Compose View appears without NullRefException. now both iOS/Android can show Tweet view with right image. Thanks a lot.

@bonnell
My app and image shows on App Cards, but I can’t tweet off
When I press down Tweet Button, it turned to grey, and a little moment later, it turned blue again.
Nothing happen on my twitter timeline

When pass the imageURI to Twitter.Compose in unity.
iOS should give a link like /XXX/YYY/ZZZ.png
Android should give a link like file:///xxx/yyy/zzz.png
so I have to do a platform-related post process for imageURI.
A bit nasty but affordable.
So now I can work through before press “Tweet” button.

After hit the “Tweet” button, I’ve got a 400 error like this.

 Unexpected response
 java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.isEmpty()' on a null object reference
 	at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
 	at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
 	at android.os.Handler.handleCallback(Handler.java:739)
 	at android.os.Handler.dispatchMessage(Handler.java:95)
 	at android.os.Looper.loop(Looper.java:135)
 	at android.app.ActivityThread.main(ActivityThread.java:5238)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at java.lang.reflect.Method.invoke(Method.java:372)
 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
 Post Tweet failed
 com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 400
 	at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
 	at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
 	at android.os.Handler.handleCallback(Handler.java:739)
 	at android.os.Handler.dispatchMessage(Handler.java:95)
 	at android.os.Looper.loop(Looper.java:135)
 	at android.app.ActivityThread.main(ActivityThread.java:5238)
 	at java.lang.reflect.Method.invoke(Native Method)
 	at java.lang.reflect.Method.invoke(Method.java:372)
 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

#12

Hello again everyone,
@bonnell @vam_si
Is there anything else I could check or try in order to post App Cards from iOS using Fabric’s Unity SDK?
We have to launch our game on iOS and we are missing this feature.

Thanks in advance.


Unexpected response when tweet App card through Fabric for Unity
#13

@andreswx the thing that stands out to me is the original NPE being thrown:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.isEmpty()' on a null object reference

It seems like some object is being dereferenced or is no longer available between the time the image is being created and the card itself but before the tweet itself is able to be composed. You could try enabling debug mode to get more information: https://docs.fabric.io/android/fabric/advanced-settings/debugging.html


#14

Hi @bonnell
I was talking about this part of @tcdnw’s post (refering to the problem on iOS, not on Android)

I’m facing the same problem.

When I call Twitter.Compose, the App Card composer shows up, but I can’t tweet.
Just as @tcdnw mentioned before, when the tweet button is pressed, it turns grey, a moment later turns blue again but nothing happens in the twitter timeline.
I have already requested and obtained permission to post app cards with the app.
We have to launch our game on iOS and we are missing this feature.
Is there anything else I could check or try in order to post App Cards from iOS using Fabric’s Unity SDK?
Thanks again.


#15

Ahh, sorry @andreswx. Just to confirm, on iOS, you’re seeing the NullReference whenever you try to tweet?


#16

No @bonnell, I’m not.

I could solve the NullReference passing a non-null value on the hashtag param of the Twitter.Compose call, like I mentioned before, just a few days ago.

After solving that, the App Card composer shows up, but I can’t post tweets.
Like I said before:

The AppCard composer shows up, but I can’t post tweets. Nothing happens. Nothing shows up on the xcode debug console either, so I have no error message to look up.
Is there anything I could check or try in order to post App Cards from iOS using Fabric’s Unity SDK?


#17

@bonnell
How can I enable debug mode in Fabric for Unity?
Your link referred to Fabric for Android.

In another case,
How could we confirm debug log on iOS?
there was an 400 error code on Android, but we saw nothing on iOS.

EDIT 1
Besides NPE, there was a Post Tweet failed frist.

Unexpected response
java.lang.NullPointerException: Attempt to invoke interface method ‘boolean java.util.List.isEmpty()’ on a null object reference
etc.
Post Tweet failed
com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 400
etc.


#18

Same problem:

Unexpected response
E Twitter : java.lang.NullPointerException: Attempt to invoke interface method ‘boolean java.util.List.isEmpty()’ on a null object reference
E Twitter : at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)

Anyone knows what’s the problem? Thanks.

Edit: In this version there is no isEmpty() code involved, I don’t know if we have a newer or older version in Fabric.


Impossible to Compose a Card with Unity SDK