Solved

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

Posted on 2009-04-08
5
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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