An object reference is required for the non-static field, method, or property

31,061

Solution 1

You need to change it to:

public static void LaunchMinecraft()

That way, the static Main method can access the static LaunchMinecraft method.

Solution 2

LaunchMinecraft is not a static method so you cannot access it in the static method Main without calling it from the Program object.

Two options
1. Make LaunchMinecraft static

public void LaunchMinecraft() 
{ ... }  

2. Create a new Program object in Main and call it that way.

var program = new Program();
program.LaunchMinecraft();
Share:
31,061

Related videos on Youtube

Michael Pfiffer
Author by

Michael Pfiffer

Updated on May 16, 2020

Comments

  • Michael Pfiffer
    Michael Pfiffer about 3 years

    I wrote a very small function to start a Java application in C# NET, but I am getting the error "An object reference is required for the non-static field, method, or property 'MinecraftDaemon.Program.LaunchMinecraft()' C:\Users\Mike\Desktop\Minecraft\MinecraftDaemon\Program.cs". I have searched other threads that suffer from the same issue but I don't understand what it means or why I am getting it.

    namespace MinecraftDaemon
    {
        class Program
        {
            public void LaunchMinecraft()
            {
                ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", "-Xmx1024M -Xms1024M -jar minecraft_server.jar nogui");
                processInfo.CreateNoWindow = true;
                processInfo.UseShellExecute = false;
    
                try
                {
                    using (Process minecraftProcess = Process.Start(processInfo))
                    {
                        minecraftProcess.WaitForExit();
                    }
                }
                catch
                {
                    // Log Error
                }
            }
    
            static void Main(string[] args)
            {
                LaunchMinecraft();
            }
        }
    }
    
    • VoodooChild
      VoodooChild over 12 years
      I think it is because LaunchMineCraft method needs an instance. Or that method should be Static.
    • VoodooChild
      VoodooChild over 12 years
      I wonder if a Compiler warning is given for this case? anyone?
  • Edi
    Edi over 12 years
    Another option, of course, is to change Main() to new Program().LaunchMinecraft();