Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Paquette 5 posts 85 karma points
    Nov 08, 2018 @ 23:46
    Dan Paquette
    0

    Cannot perform runtime binding on a null reference when passing Media Picker field into Umbraco.TypedMedia

    I'm getting "Cannot perform runtime binding on a null reference" when the following code is run.

    var mediaItem = Umbraco.TypedMedia(CurrentPage.coverImage).OfType<Image>();
    

    CurrentPage.coverImage is a Media Picker that returns the image ID.

    That said, when I manually enter the ID:

    var mediaItem = Umbraco.TypedMedia(1101).OfType<Image>();
    

    Everything works out fine.

    Any thoughts are appreciated, and thanks in advance!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 11, 2018 @ 19:58
    Alex Skrypnyk
    100

    Hi Dan

    Welcome to our friendly forum!!!

    1) You are not in the spam query anymore

    2) Try this code:

    var mediaItem = Umbraco.TypedMedia(Umbraco.AssignedContentItem.GetPropertyValue<int>("coverImage")).OfType<Image>();
    

    CurrentPage.coverImage - dynamically typed, try to avoid dynamic types.

    Thanks,

    Alex

  • Dan Paquette 5 posts 85 karma points
    Nov 12, 2018 @ 14:33
    Dan Paquette
    1

    Thanks for your help!

    I suppose it would've been more helpful to mention I was trying to use it with GetCropUrl.

    This also seemed to work for GetCropUrl:

    coverImage    = Model.Content.GetPropertyValue<IPublishedContent>("coverImage")
    

    Without needing to use Umbraco.TypedMedia.

    I'm very new to C# and Umbraco, so I'm not sure what the benefits are over one method or the other, but I'm definitely avoiding dynamic types from here on!

    Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft