Null reference issue

R

Raul

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;
}
 
R

Raul

Have you tried adding a function to check items for nulls and then, return
whatever you want, in case it's actually null?
Something like:
tvret.amortization.data[0].balanceAmount =
CheckForNull(Convert.ToDecimal("100000"));


Well, no I havent done so yet. Currently I am telling it to store
100000, so the value is definitely not null. I get the feeling that
the amortization object is not being instantiated. Even if I create a
test member, tvret.amortization.test = "100000" then I get the same
message.
 
D

David Wier

Have you tried adding a function to check items for nulls and then, return
whatever you want, in case it's actually null?
Something like:
tvret.amortization.data[0].balanceAmount =
CheckForNull(Convert.ToDecimal("100000"));


David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
 
A

Alexey Smirnov

Have you tried adding a function to check items for nulls and then, return
whatever you want, in case it's actually null?
Something like:
tvret.amortization.data[0].balanceAmount =
CheckForNull(Convert.ToDecimal("100000"));

David Wier
MVP/ASPInsider

Wow!
 
A

Alexey Smirnov

Have you tried adding a function to check items for nulls and then, return
whatever you want, in case it's actually null?
Something like:
tvret.amortization.data[0].balanceAmount =
CheckForNull(Convert.ToDecimal("100000"));

Well, no I havent done so yet. Currently I am telling it to store
100000, so the value is definitely not null. I get the feeling that
the amortization object is not being instantiated. Even if I create a
test member, tvret.amortization.test = "100000" then I get the same
message.


TVReturn tvret = new TVReturn();
tvret.amortization = new TVAmortizationSchedule();
tvret.amortization.data = new TVAmortizationData[1]; // or what ever
tvret.amortization.data[0] = new TVAmortizationData();
tvret.amortization.data[0].balanceAmount =
Convert.ToDecimal("100000");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top