locked
Getting error: threw an exception of type 'System.NullReferenceException' RRS feed

  • Question

  • User1996548659 posted

    Hi All,

    I am trying below code to with class and mentioned below how to accessing in webservice. When I create object and pass a value then getting below error,

    Please tell me how to resolve this issue? What I missed in class file or accessing?

    Error:

    res.Success.FileId = 'res.Success.FileId' threw an exception of type 'System.NullReferenceException'

    {"Object reference not set to an instance of an object."}

    Accessing with below code:

    ServiceReference1.ReportUploadResult res = new ServiceReference1.ReportUploadResult();
    res.Success.FileId = "123";
    res.Success.FileUrl = "wow/wow.pdf";

    Class file code:

    public partial class ReportUploadResult
        {

            public ReportUploadResult.SuccessMessage Success { get; set; }


            public ReportUploadResult.ErrorMessage Error { get; set; }


            public partial class SuccessMessage
            {

                public System.String FileUrl { get; set; } = "";


                public System.String FileId { get; set; } = "";

            }


            public partial class ErrorMessage
            {

                public System.String Code { get; set; } = "";


                public System.String Message { get; set; } = "";

            }
        }

    Wednesday, December 16, 2020 6:19 AM

Answers

  • User1535942433 posted

    Hi hspatil,

    Accroding to your description,as far as I think,you have set the default value to "Fileld",but you don't set the default value to "Success". "Success" will return none.So,you could set the default value to "Success".

    Just like this:

     public ReportUploadResult.SuccessMessage Success { get; set; } = new SuccessMessage();
    
            public ReportUploadResult.ErrorMessage Error { get; set; }
    
    
            public partial class SuccessMessage
            {
    
                public System.String FileUrl { get; set; } = "";
    
    
                public System.String FileId { get; set; } = "";
    
            }
    
    
            public partial class ErrorMessage
            {
    
                public System.String Code { get; set; } = "";
    
    
                public System.String Message { get; set; } = "";
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2020 2:40 AM

All replies

  • User475983607 posted

    According to the object names, you are trying to populate the response object.  The response object is returned from the service.  I would contact the service owners and ask for assistance with the service.  It seems you might not understand how the service works.

    Wednesday, December 16, 2020 1:50 PM
  • User1535942433 posted

    Hi hspatil,

    Accroding to your description,as far as I think,you have set the default value to "Fileld",but you don't set the default value to "Success". "Success" will return none.So,you could set the default value to "Success".

    Just like this:

     public ReportUploadResult.SuccessMessage Success { get; set; } = new SuccessMessage();
    
            public ReportUploadResult.ErrorMessage Error { get; set; }
    
    
            public partial class SuccessMessage
            {
    
                public System.String FileUrl { get; set; } = "";
    
    
                public System.String FileId { get; set; } = "";
    
            }
    
    
            public partial class ErrorMessage
            {
    
                public System.String Code { get; set; } = "";
    
    
                public System.String Message { get; set; } = "";
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2020 2:40 AM
  • User303363814 posted

    You will save enormous amounts of time if you can do simple very simple debugging rather than having to ask questions in internet forums.

    Set a breakpoint on the line which is throwing the exception.  Run the code.  When the breakpoint is reached examine the values of all variables.

    What is res? (It will be an object)

    What is res.Success (it will be null)

    Hmmm ... NullRefernceException and you have a reference which is Null.  Found the reason for the Exception.

    Next - why is res.Success null?  Find the code that sets res.Success to be something other than null and work out why it is not running.  In this case there is no code at all which sets res.Success to anything.  There's the problem.

    This should be the work of a few seconds.  You become hundreds (thousands) of time more productive by learning the simplest of debugging techniques.

    Thursday, December 17, 2020 2:52 AM
  • User1996548659 posted

    Hi Yijing Sun,

    Thank you very much. This answer really help me.

    Thanks,

    Harish Patil

    Thursday, December 17, 2020 4:58 AM