Null reference issue
-
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; }
-
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; }
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 )
-
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 )
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?
-
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?
-
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?
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
-
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
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]; } }