New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value cannot be null.:I2C Write Exception: {0} #6

Open
FishOfPrey opened this Issue Jun 11, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@FishOfPrey

FishOfPrey commented Jun 11, 2017

When attempting to use the nuget packages PCA9685 class I was getting the following error logged after calling SetAllPWM().

Value cannot be null.:I2C Write Exception:

Instead of the nuget package I pulled the latest source down from here. The null reference exception was coming from Device being null. At first I thought it wasn't being set correctly in InitI2CAsync, but then found that InitPCA9685Async was also throwing and logging an exception. With the exceptions being logged and then ignored it's difficult to go straight to the cause of the problem.

Then underlying error was:

Could not load file or assembly 'System.Runtime.WindowsRuntime,...

Found the solution:

the default NuGet package (Microsoft.NETCore.UniversalWindowsPlatform) had to be upgraded to the latest version (5.2.2) as well!

Once I updated UWP to 5.2.3 it started working.

@lloydjatkinson

This comment has been minimized.

Show comment
Hide comment
@lloydjatkinson

lloydjatkinson Jun 11, 2017

Contributor

I agree, logging the exception and then immediately discarding it is not the approach that should be taken, but it currently is in the method you mentioned:

    public async Task InitPCA9685Async(I2CSpeed i2cSpeed = I2CSpeed.I2C_100kHz)
    {
        try
        {
            await InitI2CAsync(i2cSpeed);

            Reset();
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            return;
        }
    }

Especially not in an "init" type method, it should either succeed or not and allow the developer to decide what happens when it does not succeed. In this case logging and rethrowing is the correct approach.

e.g:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw; // Allow consumer to decide how to handle it.
        }

or:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw new IOException("Could not initialise device.", e); 
            // Allow consumer to decide how to handle it and also throw new
            // more specific exception while preserving stack trace.
        }
Contributor

lloydjatkinson commented Jun 11, 2017

I agree, logging the exception and then immediately discarding it is not the approach that should be taken, but it currently is in the method you mentioned:

    public async Task InitPCA9685Async(I2CSpeed i2cSpeed = I2CSpeed.I2C_100kHz)
    {
        try
        {
            await InitI2CAsync(i2cSpeed);

            Reset();
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            return;
        }
    }

Especially not in an "init" type method, it should either succeed or not and allow the developer to decide what happens when it does not succeed. In this case logging and rethrowing is the correct approach.

e.g:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw; // Allow consumer to decide how to handle it.
        }

or:

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
            throw new IOException("Could not initialise device.", e); 
            // Allow consumer to decide how to handle it and also throw new
            // more specific exception while preserving stack trace.
        }
@SindreSB

This comment has been minimized.

Show comment
Hide comment
@SindreSB

SindreSB Sep 10, 2017

I just had this same issue. The project needs to be updated to UWP 5.4.0.

Solution was to download the source, and update it.

SindreSB commented Sep 10, 2017

I just had this same issue. The project needs to be updated to UWP 5.4.0.

Solution was to download the source, and update it.

@wimdecorte

This comment has been minimized.

Show comment
Hide comment
@wimdecorte

wimdecorte Jun 23, 2018

That solution did not work for me. Cloned the project into VS2107, updated the project to UWP latest 6.1.5. Getting the I2C write error and null exception on every second time it runs.
The code is run in Windows 10 IoT 10.0.17134.112 with a ThreadPoolTimer...

wimdecorte commented Jun 23, 2018

That solution did not work for me. Cloned the project into VS2107, updated the project to UWP latest 6.1.5. Getting the I2C write error and null exception on every second time it runs.
The code is run in Windows 10 IoT 10.0.17134.112 with a ThreadPoolTimer...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment