Solved

Object Class with Array

Posted on 2013-11-05
2
408 Views
Last Modified: 2013-11-05
OK everyone- I am going in too many different directions at once and I know I am missing something simple- here is my issue- I am getting an "object reference not set to an instance of an object" error and I know I a missing something obvious  below are my object classes and how I am trying to call them:

public class ItemDiscountRequest
    {
        public string ItemName;

        public int Quantity;

        public ItemDiscountRequest()
        {

        }

    }

 public class DiscountRequest
    {
        public string CustID;
       
        public ItemDiscountRequest[] Items;
           }
Then I am using them with a web method to get customer discounts---

ItemDiscountRequest discountReq = new ItemDiscountRequest();
DiscountRequest trevReq = new DiscountRequest();
           
            discountReq.ItemName = "ITEMSKU";
            discountReq.Quantity = 2;

            trevReq.Items[0] = discountReq;


            GetDiscount(trevReq);


HELP- I know I am missing something simple can someone point it out so I can feel dumb and then move on to getting things working? ;-) lol  Thanks in advance
0
Comment
Question by:jmr71
2 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 39625559
You need to provide an array item:
trevReq.Items = new ItemDiscountRequest[1];
trevReq.Items[0] = discountReq;

Open in new window


You can't assign an item to a non-initialized array. Basically:

object[] myObjectArray;

has myObjectArray = null. You need:

object[] myObjectArray = new object[someNumber];

Otherwise, you have no place to put the objects.
0
 

Author Comment

by:jmr71
ID: 39625571
ACK! I did that every which way but that- I knew it was something simple....ok walking way...lookin guilty ...awarding points....sayin thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now