0

My Xamarin cross-platform app works well in debug mode (Android), but when I change to release, it throw exceptions. I've already searched to correct for this situation. Most of posts reveal that android.permission.INTERNET needs to be added in AndroidManifest.xml file. But this doesn't work for my problem.

Additional, regardless Linker is Sdk Assembly only or None in release mode, it still don't work. (My app build on Android Pie 9.0)

And exceptions:

Unhandled Exception:
04-21 22:37:08.972 E/mono    ( 5319): System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.972 E/mono    ( 5319):   at .Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56 
04-21 22:37:08.972 E/mono    ( 5319): --- End of stack trace from previous location where exception was thrown ---
04-21 22:37:08.972 E/mono    ( 5319):   at (wrapper dynamic-method) System.Object.31(intptr,intptr)
04-21 22:37:08.972 E/mono    ( 5319):   at (wrapper native-to-managed) System.Object.31(intptr,intptr)
04-21 22:37:08.973 E/mono-rt ( 5319): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.973 E/mono-rt ( 5319):   at Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56 

<uses-permission android:name="android.permission.INTERNET" />

Below is my code when it throws exception:

public async Task<List<User>> FindExistUserAsync(string weburl, string username)
{
    string url = weburl + username;
    try
    {
        var response = await client.GetAsync(url);// throw exception after run this line.
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var JsonResult = response.Content.ReadAsStringAsync().Result;
            try
            {
                //Debug.WriteLine(JsonResult);
                var contentResp = JsonConvert.DeserializeObject<List<User>>(JsonResult);
                return contentResp;
            }
            catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
        }
    }
    catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
    return null;
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
Lotha
  • 1
  • 2
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – GSerg Apr 21 '19 at 16:34
  • On top of that, you are [blocking on async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – GSerg Apr 21 '19 at 16:36
  • Check your client, and make sure it is initialized correctly and not disposed before you use it here. – Karan Rami Apr 21 '19 at 17:01
  • 2
    You need to apply some basic debugging skills and determine which line is causing the exception, and then why? Hint: the stack trace tells you which line caused the exception – Jason Apr 21 '19 at 17:37
  • Did you use `Linking` like this url https://learn.microsoft.com/en-us/xamarin/android/deploy-test/linker,If you used it, please set `Don't Link` to exclude the `Linking` issue. – Leon Apr 22 '19 at 02:10
  • thank all for many supports. I fixed that problems. I found it here: https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted – Lotha Apr 28 '19 at 15:58
  • All right, Thanks for your sharing. – Leon Apr 30 '19 at 02:04

1 Answers1

0

I fixed that problems. I found it here:Android 8: Cleartext HTTP traffic not permitted . Starting with Android 9.0 (API level 28), cleartext support is disabled by default. Therefore, It throw exception when I try to connect with web service.

Lotha
  • 1
  • 2