locked
Why else not working with MVVM? RRS feed

  • Question

  • Hi,

    I have the lines

      homeworkJ = new HomeworkJViewModel();
      homeworkJ = homeworkJ.GetHomeworkJID(System.Convert.ToInt32(ywdwStartJ1S);

     if (homeworkJ._id > 0) // != null)
        {
            System.Diagnostics.Debug.WriteLine("homeworkJ != null ");
            // Works perfect
        }
     else //if (homeworkJ == null)
         {
             System.Diagnostics.Debug.WriteLine("homeworkJ == null ");
              //Error
             //A first chance exception of type 'System.NullReferenceException' occurred in Class Time.exe
    }

    else statment not working.
    What am I missing?

    Thanks


    ADRIAN DIBU

    Sunday, August 4, 2013 3:27 PM

Answers

  • Try this

    if (homeworkJ == null)
                {
                    // Do something here on null
                }
                else if (homeworkJ._id > 0)
                {
                    // Do something here if id is > 0
                }
                else 
                {
                    // Do something here if id is not > 0 and also not null
                }

    Also, homeworkJ value will overwritten after call to GetHomeworkJID, so it can be null or not null depending what function returns. 



    Thanks,
    Sachin

    My Samples



    Sunday, August 4, 2013 4:44 PM

All replies

  • homeworkJ must be null, that's why you are getting the null reference exception. Make your if like - if (homeworkJ != null && homeworkJ._id > 0) to fix the exception.

    Thanks,
    Sachin
    My Samples

    Sunday, August 4, 2013 3:37 PM
  • Hi,

    Thanks for the prompt answer.

    I tried same: if working else not.

    My problem is:

    I want if the result of
     homeworkJ = homeworkJ.GetHomeworkJID(System.Convert.ToInt32(ywdwStartJ1S);

    is null to do some action.

     Maybe the fact

     homeworkJ = new HomeworkJViewModel();

    gives homework is not null no mater if GetHomeworkJID is null

    Thanks


    ADRIAN DIBU

    Sunday, August 4, 2013 4:06 PM
  • Try this

    if (homeworkJ == null)
                {
                    // Do something here on null
                }
                else if (homeworkJ._id > 0)
                {
                    // Do something here if id is > 0
                }
                else 
                {
                    // Do something here if id is not > 0 and also not null
                }

    Also, homeworkJ value will overwritten after call to GetHomeworkJID, so it can be null or not null depending what function returns. 



    Thanks,
    Sachin

    My Samples



    Sunday, August 4, 2013 4:44 PM
  • Hi,

    Not working

     homeworkJ = homeworkJ.GetHomeworkJID(System.Convert.ToInt32(ywdwStartJ1S);

    If data exist all is OK.

    If no data null error.

    if (homeworkJ == null) or if (homeworkJ != null)
     Gives error because it checks if is null and it is so the error come.

    How can I pass the line when no data exist.


    ADRIAN DIBU

    Monday, August 5, 2013 12:17 AM
  • Hi,

    I tried

      homeworkJ = new HomeworkJViewModel();

      if (homeworkJ != null)
                    {
                        System.Diagnostics.Debug.WriteLine("");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("homeworkJ == null");
                   

    I got

    homeworkJ != null

    means even no data homeworkJ is not null.

     So why the lack of data in

    homeworkJ = homeworkJ.GetHomeworkJID(System.Convert.ToInt32(ywdwStartJ1S);

    gives null error and impossibility to do something if no data.

    Thanks


    ADRIAN DIBU

    Monday, August 5, 2013 12:40 AM
  • Sorry, I am not able to understand it. If possible, upload a small project where you are facing the issue and what should be expected results.

    Thanks,
    Sachin
    My Samples

    Monday, August 5, 2013 2:21 AM