• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

C# Script problem: Null?

Status
Not open for further replies.
Level 17
Joined
Dec 11, 2014
Messages
2,004
I'm new to these stuff, trying to create a small app for my school, getting an error at the time of entry.

Code:
namespace meh
    public class Globals
    {
        public static string[] StdName;
        public static int Max;
    }
    public partial class MainForm : Form // ye I'm using VS forms which means this is the entry point instead of Main()
    {
        public MainForm()
        {
            InitializeComponent();
            new Globals();
            int i = 1;
            Globals.Max = 28;
            Globals.StdName[i] = "s"; //error here
        }
}

The error:
"An unhandled exception of type 'System.NullReferenceException' occurred in ThisApplication.exe

Additional information: Object reference not set to an instance of an object."

I seriously don't understand why StdName has a null reference. i is 1. Globals is a class. StdName is a string array and the value is correct. What's the problem?? Even worse, Globals.Max does not throw an error.

Any ideas?

QUICK EDIT: Removing the array makes it work. With the array it does not work, it throws the null error. Putting Globals.StdName[1] = "s"; also doesn't work; which means the whole problem is the array. Using struct/class instances instead of arrays makes it harder, don't say that please :p
 
Status
Not open for further replies.
Top