An Object Reference Is Required for the Non-Static Field

by Liam Cubicle Published April 2, 2024

The “object reference is required for the non-static field” error arises when you attempt to access a non-static field without the prior creation of an object instance. This error is peculiar to object-oriented programming languages.

This article will serve as a guide to fixing this error in C#.

First, let us understand what a non-static field means.

What is a non-static field?

A non-static field or variable is specific to individual objects. Hence, requires an object instance. As opposed to the static field/variable that can be accessed directly without the need for an object instance because it belongs to the class itself.

Fixing the error

Consider the following C# example to demonstrate the error;

using System;

public class DisplayName // Defines class
{
    private int myName; // Non-static field

    public void MyMethod() // Non-static method to display name
    {
        Console.WriteLine(“Non-static method called”)
    }

  public static void Main(string[] args)
  {
    Console.WriteLine(myName) // Attempting to access the non-static field
    MyMethod(); // Attempting to access the non-static method
  }
}

In the above example, the class DisplayName is defined. The class contains a non-static field and method - myName and myMethod respectively. In the Main method, the non-static field and method were tried to access in a static context.

Attempting to access the non-static field in a static context, will result in this error: An object reference is required for the non-static field, method, or property DisplayName.myName

Attempting to access the non-static method in a static context, will result in this error: An object reference is required for the non-static field, method, or property DisplayName.MyMethod()

Now, to resolve this error;

First, create an object instance.

DisplayName newObject = new DisplayName();

Doing this, you can now access its non-static field and method.

Console.WriteLine(newObject.myName);
newObject.MyMethod();

Now, accessing the non-static field and method will run seamlessly without any object reference error.

In addition, you should check the object for null references before accessing its members.

if (newObject != null)
{
    Console.WriteLine(newObject.myName);
}
else
{
    Console,WriteLine(“Object is null!”);
}

In summary, to resolve the error “an object reference is required for the non-static field”, you need to create an object instance before accessing non-static members and also ensure you handle null references.

Alternatively, you can make the field or method static so it does not require an object instance to be accessed. Like this;

using System;

public class DisplayName
{
    private static int staticField;

    public static void StaticMethod()
    {
        Console.WriteLine("Static method called");
    }
}

In the above example, a static field, staticField, and a static method, StaticMethod is called. With this, you do not need to create an object instance to access it.

By following the above guide, you should be able to effectively fix the error in your C# code.

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free