1

I'm getting a NullReferenceException and I really have no idea on why it is happening. The Exception is thrown inside my EventHandler for the DataRequested method of the DataTransferManager. Here's the method:

void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    DataRequest request = args.Request;

    // The Title is mandatory
    request.Data.Properties.Title = TranslationManager.GetLocalizedString("SomeTitle");

    // SharedString is never null, I get it with a ?? String.Empty
    request.Data.SetText(SharedString);
}

And this is StackTrace:

at Windows.ApplicationModel.DataTransfer.DataPackage.SetText(String value)

at OneLocker.Views.CardPage.dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)

To add the EventHandler, I just do the usual:

DataTransferManager DataTransferManager = DataTransferManager.GetForCurrentView();
DataTransferManager.DataRequested += dataTransferManager_DataRequested;

What's bugging me is that the EventHandler works fine most of the times it is called, it's just crashing for some users of my app. Also, I really don't get what is causing the Exception. The request variable is taken from the event args, so how is it possible to be null? I saw the code examples on the MSDN site, and they never did some kind of error checking there, they just assumed that it would never be null. So how is it that it is crashing here?

Thank you so much for your help!

Sergio

Community
  • 1
  • 1
Sergio0694
  • 4,447
  • 3
  • 31
  • 58
  • Did you consider that the message is right? That `SharedString` is indeed null? Check the value before calling `SetText` – Panagiotis Kanavos Jul 14 '15 at 12:23
  • @PanagiotisKanavos Yeah I did consider it, but if that String was null I'd get an ArgumentNullException, not a NullReferenceException. So it really looks like that request.Data is actually null, but I have no clue on how is it possible. – Sergio0694 Jul 14 '15 at 12:25
  • I'd like to THANK the user who marked the question as duplicate, as he didn't bother to actually READ the question. If the String was null I'd have got an ArgumentNullException and NOT a NullReferenceException. Maybe spend some time reading the text before flagging questions next time? – Sergio0694 Jul 14 '15 at 12:28
  • if `request.Data` was null, then `request.Data.Properties.Title = ...` wouldn't work and the stack trace would look different. – Corak Jul 14 '15 at 12:28
  • 1
    You never told us that you check `SharedString` and throw an `ArgumentNullException` in your question. *You* know you do; *we* don't. -- you just say you "get it with a ?? String.Empty". – Corak Jul 14 '15 at 12:30
  • @Corak Yeah, I get it with a ?? String.Empty, so the value I get can never be null. I mean, SharedString is a property that never returns a null value, so I'm just saying that can't be the cause of the NullReferenceException. And I didn't say I manually throw an ArgumentNullException, I meant that the SetMethod itself throws that if it's called with a null String – Sergio0694 Jul 14 '15 at 12:33
  • The stack trace says the exception happens inside `Windows.ApplicationModel.DataTransfer.DataPackage.SetText(String value)`. If you don't have access to that method and are sure you're providing a valid value, then it's out of your control and you probably need to find a way to gracefully recover to a valid state again. Because crashing the app doesn't seem to be desirable. – Corak Jul 14 '15 at 12:40
  • @Corak Yeah, I was just wondering if this was a known bug in WinRT or if anyone else had this problem before. I'm sure that I'm not passing a null value, that's why I have no idea what's going on here. Yeah I guess I'll just throw that line inside a try catch block and just show a message box to inform the user in case the method fails. I'd just like to know what is causing the issue – Sergio0694 Jul 14 '15 at 12:43
  • Ensure that you don't set a null title *and* that SharedString you don't pass a null argument to SetText. You may be asking for a missing resource, or there may by a type in the way you set SharedString. You don't post the relevant code so it's impossible to replicate the problem. – Panagiotis Kanavos Jul 14 '15 at 12:45
  • Also check the contents of `request.Data` by setting a watch variable in the debugger – Panagiotis Kanavos Jul 14 '15 at 12:47
  • @PanagiotisKanavos Actually SharedString is never null, as I've explained before, and the resource isn't null, I checked that, and the method that retrieves it just returns an empty String in case the resource is missing (which is not, by the way). – Sergio0694 Jul 14 '15 at 13:06

0 Answers0