im getting this error when deploy a APK maked in Xamarin.Android (From Logcat of my device)
>*android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
at MyApp.InvoiceActivity.SendJson (System.String Json, System.String Route, System.String Token) [0x00053] in <f72bff8dbb074291a532e12425c8737a>:0
at MyApp.InvoiceActivity.OnCreate (Android.OS.Bundle bundle) [0x0004a] in <f72bff8dbb074291a532e12425c8737a>:0
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <0181fdcaff164e668b24279eb4fb429f>:0
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr,intptr)
at crc64df67992c1ed0ef67.InvoiceActivity.n_onCreate(Native Method)
at crc64df67992c1ed0ef67.InvoiceActivity.onCreate(Unknown Source:0)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)*
This error says that my problem start in sendJson() method, this is the snippet of this method
public string SendJson(string Json, string Route = "MyRoute", string Token = "MyToken")
{
try
{
using var client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = "Token token=" + Token;
string response = client.UploadString(Route, "POST", Json);
return response;
}
catch (WebException ex)
{
var response = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
return response;
}
}
But, this error only happens when the app is instaled and executed from the exported APK. When i use the Visual Studio Debugger, the method work well.
I tried to put the method in a isolated class or inside the Activity (even put part of the method inside onCreate() method), change level access, but nothing works.
EDIT
Thanks to all for answer and give me some ideas about this problem. After making some test i discover that the problem ocurr when "Optimize code" is enabled in Compilation section on Project properties. In Xamarin.Android, to export a signed APK you need first set project in "Release" mode to make that and, of course, this option is enabled by default ther. Im not sure if this is a bug of compilator or is just my fault, so im gonna keep investigating :). Anyways, thanks so much again for the help.