1

I try to connect to a specific Wifi but everytime I get a System.NullReferenceException: 'Object reference not set to an instance of an object.' (commented in code)

Here is my method

public static Task ConnectToWifi(Wifi wifi)
        {
            var wifiSpecififier = new WifiNetworkSpecifier.Builder()
                .SetSsid(wifi.SSID)
                .SetWpa2Passphrase(wifi.Password)
                .SetIsHiddenSsid(wifi.Hidden)
                .Build();

            var request = new Android.Net.NetworkRequest.Builder()
                .AddTransportType(Android.Net.TransportType.Wifi)
                .RemoveCapability(Android.Net.NetCapability.Internet)
                .SetNetworkSpecifier(wifiSpecififier)
                .Build();

            var connectivityManager = Android.App.Application.Context.GetSystemService(
                Android.App.Application.ConnectivityService) as Android.Net.ConnectivityManager;

            connectivityManager.RequestNetwork(request, new NetworkCallback()); //nullex

            return Task.CompletedTask;
        }

Here is my NetworkCallback class

public class NetworkCallback : Android.Net.ConnectivityManager.NetworkCallback
    {
        public static Android.Content.Context context = Android.App.Application.Context;

        Android.Net.ConnectivityManager connectivityManager = (Android.Net.ConnectivityManager)context.GetSystemService(Android.App.Application.ConnectivityService);

        public override void OnAvailable(Android.Net.Network network)
        {
            base.OnAvailable(network);
            connectivityManager.BindProcessToNetwork(network);
        }

        public override void OnUnavailable() => base.OnUnavailable();
    }

Edit:

at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr)

11
  • if you know which line causes the exception, then you need to determine which element on that line is null. We can't do that for you. Use the debugger or examine the stack trace for more info Commented May 26, 2022 at 19:34
  • Good question lol i debug this an nothing is null Commented May 26, 2022 at 19:46
  • Obviously something is. Look at the stack trace to see what the root cause is Commented May 26, 2022 at 19:47
  • request = {NetworkRequest [ NONE id=0, [ Transports: WIFI Capabilities: NOT_RESTRICTED&TRUSTED&NOT_VPN Specifier: <WifiNetworkSpecifier [, SSID Match pattern=PatternMatcher{LITERAL: Fu5704Wolke}, BSSID Match pattern=Pair{00:00:00:00:00:00 00:00:00:00:00:00}, SSID="Fu5704Wolke", BSSID=null]> Uid: 10154 AdministratorUids: [] RequestorUid: -1 RequestorPackageName: null] ]} AND new NetworkCallback() = {crc64c1fb061c2ccb06a5.NetworkCallback@1671fdb} Commented May 26, 2022 at 19:48
  • My stacktrace: at Android.Runtime.JNINativeWrapper._unhandled_exception (System.Exception e) [0x0000e] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V (_JniMarshal_PPL_V callback, System.IntPtr jnienv, System.IntPtr klazz, System.IntPtr p0) [0x0001d] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:111 at (wrapper native-to-managed) Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(intptr,intptr,intptr) Commented May 27, 2022 at 13:22

1 Answer 1

0

Make sure to add any required permissions, I think you need this to your AppManifest to fix the error:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.