I'm trying to learn about statics ... let's say in the code below, I just need these 30 dogs to be available throughout my program.

What am I doing wrong here? It builds fine, but I'm getting the runtime error at line 15:

An unhandled exception of type System.NullReferenceException occurred in ConsoleApplication1.exe
Additional information: Object reference not set to an instance of the object.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string aName;
            for (int x = 0; x < 30; x++)
            {
                aName = AppUtils.myDogs[x].Name;  //<---- Errors here
            }
        }
    }

    public class MyDogClass
    {
        private string name;
        private string breed;
        private int age;

        public MyDogClass()
        {
            this.name = "Unknown";
            this.breed = "Unknown";
            this.age = 0;
        }

        public string Name{  get{ return this.name;}  set{this.name = value;}}
        public string Breed{ get{ return this.breed;} set{this.breed = value;}}
        public int    Age{   get{return this.age;}    set{this.age = value;}}

    }

    public static class AppUtils
    {
        public static MyDogClass[] myDogs = new MyDogClass[30];
    }
}

Thanks guys. Its coming along slow ... but thanks to you guys, its starting to gel. Its just that I hit stuff like this every once ins a while.

Recommended Answers

All 13 Replies

You never instantiate AppUtils.

You wrote the class for it but never actually create an instance of the class as such:

AppUtils myUtils = new AppUtils();

All class objects need to be instanciated prior to use.

Your usage would then be:

aName = myUtils.myDogs[x].Name;

Hope that helps :) Please remember to mark the thread solved once your issue is resolved.

Edit: Disregard this, Lusiphur's Red Bull cycle hadn't completed yet and his brain hit 'standby' mode... Was thinking regular (not static) classes when I typed this :twisted:

You never instantiate AppUtils.

You wrote the class for it but never actually create an instance of the class as such:

AppUtils myUtils = new AppUtils();

All class objects need to be instanciated prior to use.

Your usage would then be:

aName = myUtils.myDogs[x].Name;

Hope that helps :) Please remember to mark the thread solved once your issue is resolved.

Lusiphur

(LOL! I've seen your name a 1000 times up here as I read responses ... now that I've typed it, I get it! Hahaha ... went right over my head)

Hmmmm, ok, I thought the idea behind a static class (remember, I'm still learning) was; you don't have to instance it, and that you shouldn't.

But further, I'm confused because -if- I instance it, doesn't that create an array local to the class its instantiated in? And when I instance it in another class in my app, that would be a whole different instantiated array?

Sorry for being so dense.

You can't call class members without making an instance of it.

Try adding a constructor to AppUtils class and instantiating all the array elements there:

public static class AppUtils
    {
        public static MyDogClass[] myDogs = new MyDogClass[30];

[B]       static AppUtils()
        {
            for (int i = 0; i < myDogs.Length; i++)
            {
                myDogs[i] = new MyDogClass();
            }
        }[/B]
    }

You never instantiate AppUtils.

You wrote the class for it but never actually create an instance of the class as such:...

Lusiphur you are mistaken, mate. AppUtils is a static class. You can't make an instance of a static class.

commented: Darn you for being RIGHT!! :twisted: +1

My bad, but the fact remains that as you stated:
>>You can't call class members without making an instance of it.

My methodology may be a bit off having been kept awake all night last night :zzz:

Hmmmm, ok, I thought the idea behind a static class (remember, I'm still learning) was; you don't have to instance it, and that you shouldn't.

No, you and farooqaaa are right, this would be a prime example of my brain going into 'standby' mode right in the middle of my Red Bull cycle :P

You can't call class members without making an instance of it.

Try adding a constructor to AppUtils class and instantiating all the array elements there:

public static class AppUtils
    {
        public static MyDogClass[] myDogs = new MyDogClass[30];

[B]       static AppUtils()
        {
            for (int i = 0; i < myDogs.Length; i++)
            {
                myDogs[i] = new MyDogClass();
            }
        }[/B]
    }

Lusiphur you are mistaken, mate. AppUtils is a static class. You can't make an instance of a static class.

OMG farooqaaa! It was right there in front of my face! That so fixed it! Thank you, thank you, thank you!!!

I spent 3 days beating my head against the wall trying to figure out what I was doing wrong!

Thank you again, both of you!!!!!

commented: Always nice to see gratitude... even when I'm wrong :P +1

OMG farooqaaa! It was right there in front of my face! That so fixed it! Thank you, thank you, thank you!!!

I spent 3 days beating my head against the wall trying to figure out what I was doing wrong!

Thank you again, both of you!!!!!

Well, oddly enough, farooqaaa's solution worked in the console application I wrote to show the problem, it doesn't work in the windows app (that is far too large to post here).

It won't let me put a constructor in the static class in the Windows app (but it did in the console app ... go figure).

But at least you guys got me going in the right direction to research and learn more.

What do you mean it doesn't let you put a constructor? What error are you getting?

Keeping in mind that my Red Bull function appears to require a higher value of sleep than I've input... (I've had 2 hours sleep so I'm a bit groggy)...

Might I ask where you're setting your classes up for your win-app? Are you doing so in the program.cs or the formNameHere.cs file?

int alertness = RedBull(sleep, ml)

public int RedBull(int sleepHours, int canSize)
{
    int alertness = 0; //Range 1-10
    if (sleepHours < 5)
    {
        alertness = 1;
        return alertness;
    }
    else
    {
        //insert crazy complicated calculation based on sleehours and canSize
        //return alertness more appropriate to answering questions
    }
}

Well, oddly enough, farooqaaa's solution worked in the console application I wrote to show the problem, it doesn't work in the windows app (that is far too large to post here).

It won't let me put a constructor in the static class in the Windows app (but it did in the console app ... go figure).

But at least you guys got me going in the right direction to research and learn more.

Ok ... figured it out ...

When making a constructor, I already have myself trained to make it Public ... but in a static class, it has to be STATIC.

My bad ... I cut and pasted farroooga's solution into my console app ... but typed it into my windows app and used public instead of static.

Like I said, you guys rock!!!!!!

Okay

Hmmm, for some reason, my last post didn't take. My apologies.

I figured out the problem ...

I created a console app to demonstrate the problem I was having, and I copied and pasted Farrooqa's solution to that console application and it worked.

When I tried to apply it to my windows app, I typed it in, and out of habit I declared the constructor as public instead of static. It rejected that, and after studying the solution for a few moments, I keyed in on the static declaration for the constructor instead of the public declaration ... changing it to static made everything work awesome.

I am still not quite sure why the constructor would have to be declared as static, but I'm not worrying about it at this point.

Thanks again guys!!!! You rock.

And Lusiphur ... the code snippet made me laugh! Thank you for that. It was a nice relief from my frustration of the last couple of days. :)

And Lusiphur ... the code snippet made me laugh! Thank you for that. It was a nice relief from my frustration of the last couple of days. :)

No problem :) That was kinda the point of it hehe. I'm just amazed it got it 'right' considering how tired I was. On the bright side, I went and had a nap so I'm feeling MUCH better now :twisted:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.