Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Null reference issue

Null reference issue

Scheduled Pinned Locked Moved C#
helptestingbeta-testing
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abbas82
    wrote on last edited by
    #1

    Hi, I am having a problem of storing a string into TVReturn.amortization.data[0].balanceAmount (class definitions are given below), where I get the error message saying: System.NullReferenceException: Object reference not set to an instance of an object at this line where I am testing inserting values into the amortization schedule data tvret.amortization.data[0].balanceAmount = Convert.ToDecimal("100000"); I have the following class definitions: public class TVReturn { public decimal unknownEventAmount; public double unknownNominalAnnualRate; public int unknownEventNumber; public string cashFlowDataXml; public TVAmortizationSchedule amortization; } public class TVAmortizationSchedule { public TVAmortizationData[] data; } public class TVAmortizationData { public int sequenceNumber; public string eventDate; public decimal paymentAmount; public decimal interestAmount; public decimal principalAmount; public decimal balanceAmount; }

    C 1 Reply Last reply
    0
    • A Abbas82

      Hi, I am having a problem of storing a string into TVReturn.amortization.data[0].balanceAmount (class definitions are given below), where I get the error message saying: System.NullReferenceException: Object reference not set to an instance of an object at this line where I am testing inserting values into the amortization schedule data tvret.amortization.data[0].balanceAmount = Convert.ToDecimal("100000"); I have the following class definitions: public class TVReturn { public decimal unknownEventAmount; public double unknownNominalAnnualRate; public int unknownEventNumber; public string cashFlowDataXml; public TVAmortizationSchedule amortization; } public class TVAmortizationSchedule { public TVAmortizationData[] data; } public class TVAmortizationData { public int sequenceNumber; public string eventDate; public decimal paymentAmount; public decimal interestAmount; public decimal principalAmount; public decimal balanceAmount; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Abbas82 wrote:

      TVAmortizationData[] data;

      This creates an array. Every object in that array is null, until you call new on it. This is why you're getting this error.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      A 1 Reply Last reply
      0
      • C Christian Graus

        Abbas82 wrote:

        TVAmortizationData[] data;

        This creates an array. Every object in that array is null, until you call new on it. This is why you're getting this error.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        A Offline
        A Offline
        Abbas82
        wrote on last edited by
        #3

        Christian Graus wrote:

        This creates an array. Every object in that array is null, until you call new on it. This is why you're getting this error.

        How would I call the new on it? I instantiate the TVReturn object like so TVReturn tvret = new TVReturn(); what would be my next step?

        J J 2 Replies Last reply
        0
        • A Abbas82

          Christian Graus wrote:

          This creates an array. Every object in that array is null, until you call new on it. This is why you're getting this error.

          How would I call the new on it? I instantiate the TVReturn object like so TVReturn tvret = new TVReturn(); what would be my next step?

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          You must initialize data like so:

          data = new TVAmortizationData [10 /* 10 is arbitrary, make the array whatever size you want */];

          you'll probably want to do this in the constructor of the class TVAmortizationSchedule

          A 1 Reply Last reply
          0
          • A Abbas82

            Christian Graus wrote:

            This creates an array. Every object in that array is null, until you call new on it. This is why you're getting this error.

            How would I call the new on it? I instantiate the TVReturn object like so TVReturn tvret = new TVReturn(); what would be my next step?

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            TVAmortizationData[] data = new TVAmortizationData[10]; // replace with how many elements you want in the array.
            data[0] = new TVAmortizationData();
            ...

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: The "Church" at Mt. Sinai The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            1 Reply Last reply
            0
            • J Jimmanuel

              You must initialize data like so:

              data = new TVAmortizationData [10 /* 10 is arbitrary, make the array whatever size you want */];

              you'll probably want to do this in the constructor of the class TVAmortizationSchedule

              A Offline
              A Offline
              Abbas82
              wrote on last edited by
              #6

              jimmanuel wrote:

              You must initialize data like so: data = new TVAmortizationData [10 /* 10 is arbitrary, make the array whatever size you want */];

              I changed the class to the following, but I still get the same message. public class TVAmortizationSchedule { public TVAmortizationData[] data; public TVAmortizationSchedule() { data = new TVAmortizationData[5]; } }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups