Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 19th, 2007, 12:27 PM
Authorized User
Join Date: Jul 2006
Location: , , .
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default Null reference issue


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;

Old March 19th, 2007, 11:39 PM
Friend of Wrox
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts

You must first make an instance of an object before you can use it.
In your case, yoiu actually need to instantiate 2 objects before you can attempt to use them (your tvret - which would be an instance of the TVReturn class, and its member amortization - which is an instance of a instance TVAmortizationSchedule class). You'll also need a way to make an instance of TVAmortizationData and put it into the "data" array that you are apparently trying to use as the only member of your TVAmortization class. You'll need a way to dimension the "data" array as well... or at least I think you will. Does this code compile? Apparently it does, but you have a little work to do before this will actually do anything.

Woody Z
How to use a forum to help solve problems

Similar Threads
Thread Thread Starter Forum Replies Last Post
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
Null reference exception when using DataAdapter Samantha Karen Webb C# 1 July 21st, 2006 09:12 AM
NULL reference exception Dwizz VB.NET 2002/2003 Basics 3 June 21st, 2005 07:15 AM
Problems in ViewStates[]. Null Reference? chenzw BOOK: ASP.NET Website Programming Problem-Design-Solution 0 April 6th, 2004 08:01 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.