Solved

Help with C# classes - Object reference not set to an instance of an object.

Posted on 2009-04-08
5
181 Views
Last Modified: 2012-05-06
Hi all. I have some C# code here that compiles and it reads pretty easily. But although it compiles, it crashes on startup and when I tell it to debug it says "Object reference not set to an instance of an object." at fillParamArray().

Does anyone know how to fix what I'm doing? It seems pretty straightforward...
using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Diagnostics;

using System.IO;

using System.Linq;

using System.Net;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Channels;

using System.ServiceModel.Description;

using System.ServiceModel.Dispatcher;

using System.ServiceModel.Web;

using System.Text;

using System.Web;

using System.Xml;
 

public class CParamInfo

{

    public string LabelText;

    public long Value;

}
 

public class getString

{

    static CParamInfo[] globalParams = new CParamInfo[17];
 

    public static string getParameterInfo(int parameterID)

    {

        string LabelText;

        long Value;
 

        /* String that will contain all values separated by | characters */

        string fullValue;
 

        LabelText = globalParams[parameterID].LabelText;

        Value = globalParams[parameterID].Value;
 

        fullValue = "|ok|" + TitleText + "|" + LabelText + "|";
 

        return fullValue;

    }
 

    static void fillParamArray()

    {

        globalParams[0].LabelText = "Title for 0";

        globalParams[0].Value = 45;
 

        globalParams[1].LabelText = "Title for 1";

        globalParams[2].Value = 65;
 

        globalParams[3].LabelText = "Title for 2";

        globalParams[3].Value = 100;

    }
 

    public static void Main()

    {

        /* Fill in the Param Array values */

        fillParamArray();
 

        Console.WriteLine("Top of Screen Printout\n");
 

        // Do other stuff here that calls other functions, not important for this problem
 

    }

}

Open in new window

0
Comment
Question by:ehensens
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:Chuck Yetter
ID: 24100958
Initialize each CParamInfo object in the array using new.
0
 

Author Comment

by:ehensens
ID: 24100975
Hi Axshun,

Sorry, but can you be a little more specific? I'm not positive what you mean (example?)
0
 
LVL 15

Accepted Solution

by:
spprivate earned 250 total points
ID: 24101029
You have to create
globalParams[0] = new CParamInfo(); before each param to assing value.
 globalParams[0] = new CParamInfo();        

globalParams[0].LabelText = "Title for 0";

        globalParams[0].Value = 45;

 globalParams[1] = new CParamInfo(); 

        globalParams[1].LabelText = "Title for 1";

globalParams[2] = new CParamInfo(); 

        globalParams[2].Value = 65;

 globalParams[3] = new CParamInfo(); 

        globalParams[3].LabelText = "Title for 2";

        globalParams[3].Value = 100;

Open in new window

0
 
LVL 6

Assisted Solution

by:Chuck Yetter
Chuck Yetter earned 250 total points
ID: 24101062
Your array contains null pointers to CParamInfo objects.  You need to initialize each element in the array.
Loop through the array and set each array element to a new object:

for(int i =0; i < arraysize; i++)
{
    globalParams[i] = new CParamInfo();//may need arguments in constructor call
}
0
 

Author Closing Comment

by:ehensens
ID: 31568207
Thank you to you both
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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
XML & .net 5 40
Page navigation in windows phone 8.1 application not working. 5 19
Wav problem 4 13
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

22 Experts available now in Live!

Get 1:1 Help Now