none
Object reference not set to an instance of an object? RRS feed

  • Question

  • I recently got this error on visual studio 2019. 

    Object reference not set to an instance of an object.

    How do I fix this


    • Edited by TalonHero Sunday, March 15, 2020 9:11 PM
    Sunday, March 15, 2020 9:11 PM

Answers

  • Hi TalonHero,

    Thank you for posting here.

    The declaration and instance of an object are two things. An instance of an object is stored on the heap, and the object's declaration is used to point to the address of its instance on the heap.

    For example, you have a house and then provide your home address to your friends. He finds your house at this address and can then use the house's various functions normally. However, if your house does not exist and the address points to a blank, he will definitely make a mistake if he still wants to use the house.

    This example is not particularly suitable, but the ideas are similar. That is, you have a class in your code and you did not instantiate it with "new", but trying to use it results in an error.

    If you still don't understand, please post your code and we will try to resolve this error.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by TalonHero Monday, March 16, 2020 4:16 AM
    Monday, March 16, 2020 2:07 AM
  • If maybe someone would want to do a live share that would help too!

    Do you know how to create archive files, such as .ZIP, of a directory and all
    subdirectories?

    If you do, then an option used by some developers is to zip up the project directory
    that is failing, together with its subdirectories. They then post that archive 
    on a file sharing service such as Microsoft's OneDrive.

    https://products.office.com/en-us/onedrive/online-cloud-storage

    OneDrive Basic 5 GB is free. You can log in with the same account/password
    used for these forums.

    After storing the archive there, you can post a link to it in a reply in this
    thread. Then anyone who wishes to examine it can download the archive and
    see what is happening first hand.

    - Wayne


    • Edited by WayneAKing Monday, March 16, 2020 2:17 AM
    • Marked as answer by TalonHero Monday, March 16, 2020 4:16 AM
    Monday, March 16, 2020 2:16 AM

All replies

  • What exactly do you mean and which line of code produced this error?

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Sunday, March 15, 2020 9:34 PM
    Moderator
  • This is the call stack

    at System.Resources.Tools.StronglyTypedResourceBuilder.Create(IDictionary resourceList, String baseName, String generatedCodeNamespace, String resourcesNamespace, CodeDomProvider codeProvider, Boolean internalClass, String[]& unmatchable)
    at System.Resources.Tools.StronglyTypedResourceBuilder.Create(IDictionary resourceList, String baseName, String generatedCodeNamespace, CodeDomProvider codeProvider, Boolean internalClass, String[]& unmatchable)
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.BuildType()
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.GetObjectType()
    at Microsoft.VisualStudio.Shell.Design.GlobalType.get_ObjectType()
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObject.get_Children()
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjectsForItem(ProjectItem item, GlobalObjectCollection oldObjects, GlobalObjectCollection newObjects, ITypeResolutionService typeResolver)
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjectsForItem(ProjectItem item, GlobalObjectCollection oldObjects, GlobalObjectCollection newObjects, ITypeResolutionService typeResolver)
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.CreateGlobalObjects(Project project)
    at Microsoft.VisualStudio.Design.Serialization.ResXGlobalObjectProvider.GetGlobalObjectsCore(Project project, Type baseType)
    at Microsoft.VisualStudio.Shell.Design.GlobalObjectProvider.GetGlobalObjects(Project project, Type baseType)
    at Microsoft.VisualStudio.Shell.Design.GlobalObjectService.GetGlobalObjects(Type baseType)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetTypeFromGlobalObjects(String name, Boolean throwOnError, Boolean ignoreCase)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.AggregateTypeResolutionService.GetType(String name)
    at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetRuntimeType(String typeName)
    at System.ComponentModel.Design.Serialization.DesignerSerializationManager.GetType(String typeName)
    at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.GetType(String typeName)
    at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.GetType(IDesignerSerializationManager manager, String name, Dictionary`2 names)
    at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.FillStatementTable(IDesignerSerializationManager manager, IDictionary table, Dictionary`2 names, CodeStatementCollection statements, String className)
    at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    --- End of stack trace from previous location where exception was thrown ---
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload) 

    Sunday, March 15, 2020 10:07 PM
  • You can't use the visual stuido debugger and start single-stepping until it blows up? The error message means that code is trying to use/reference an object that is  a null valued object it's not there in memory.

    Learn how to use Quickwatch and find out what object is null on the line of code that blew-up. And then find out  why the object is null.

    Sunday, March 15, 2020 10:31 PM
  • In WPF there are a few things you can do right off the bat to check...

    1. Does the XAML window name match its class name

    2. Is the start object (Properties) an old/renamed window

    3. Clean/Rebuild/Build

    Hope that helps.


    George Frias - AWWshop @ Wikidot, Wiki1978





    • Edited by GeoFrias Sunday, March 15, 2020 10:37 PM
    Sunday, March 15, 2020 10:36 PM
  • IDK what any of that means is there any way yall can tell me what to do?
    Sunday, March 15, 2020 10:50 PM
  • Greetings TalonHero.

    That error means that you are trying to use an object without having instantiated it (using the 'new' keyword). That's why it says you don't have an 'instance' of it.

    That is, you have done something like this...

    MyObject mo;
    mo.DoSomething();

    ...instead of this.

    MyObject mo = new MyObject();
    mo.DoSomething();

    Sometimes errors of this kind can be difficult to track down, because they can be spread over several lines and not be as obvious as that example, but that's the general idea.


    Sunday, March 15, 2020 11:19 PM
  • I thank yall for trying to help but I still dont know how to fix
    Sunday, March 15, 2020 11:41 PM
  • https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Everything in OO programming follows the above principles. If a code tries to access an object,  and the object has not been instanced, then it's  'Object reference not set to an instance of an object'.

    Learn how to debug your code and find out what object has not been instanced.

    https://michaelscodingspot.com/debugging-part1/

    Once you find out what object is not an instance, debug your code and find out why.

    As a matter of fact, if you start the program in debug mode and continue to run it, then the debugger will take you to the line that blows-up.

    Monday, March 16, 2020 12:59 AM
  • Hello,

    • What type of project is this e.g. ASP.NET, WPF, Window Forms etc ?
    • Is this a brand new project?
    • Where is the error being thrown? e.g. when something loads like a form or a click event ? 
    • Have you tried commenting out code and re-trying ?
    • If happening in the load event then comment out the load event code.
    • If XAML based (WPF) this may need you to inspect data bindings are properly done as it[s possible to compile with some XAML errors which don't resolve until runtime.

    So how to solve the problem, the first step is to answer the questions above.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 16, 2020 1:08 AM
    Moderator
    • It is a Windows form
    • Yes it is
    • I dont really know
    • Yes
    • Dont know what that means
    Monday, March 16, 2020 1:14 AM
  • If maybe someone would want to do a live share that would help too!
    Monday, March 16, 2020 1:16 AM
  • Hi TalonHero,

    Thank you for posting here.

    The declaration and instance of an object are two things. An instance of an object is stored on the heap, and the object's declaration is used to point to the address of its instance on the heap.

    For example, you have a house and then provide your home address to your friends. He finds your house at this address and can then use the house's various functions normally. However, if your house does not exist and the address points to a blank, he will definitely make a mistake if he still wants to use the house.

    This example is not particularly suitable, but the ideas are similar. That is, you have a class in your code and you did not instantiate it with "new", but trying to use it results in an error.

    If you still don't understand, please post your code and we will try to resolve this error.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by TalonHero Monday, March 16, 2020 4:16 AM
    Monday, March 16, 2020 2:07 AM
  • If maybe someone would want to do a live share that would help too!

    Do you know how to create archive files, such as .ZIP, of a directory and all
    subdirectories?

    If you do, then an option used by some developers is to zip up the project directory
    that is failing, together with its subdirectories. They then post that archive 
    on a file sharing service such as Microsoft's OneDrive.

    https://products.office.com/en-us/onedrive/online-cloud-storage

    OneDrive Basic 5 GB is free. You can log in with the same account/password
    used for these forums.

    After storing the archive there, you can post a link to it in a reply in this
    thread. Then anyone who wishes to examine it can download the archive and
    see what is happening first hand.

    - Wayne


    • Edited by WayneAKing Monday, March 16, 2020 2:17 AM
    • Marked as answer by TalonHero Monday, March 16, 2020 4:16 AM
    Monday, March 16, 2020 2:16 AM
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using WeAreDevs_API;
    
    namespace RoPloit
    {
        public partial class RoPloit : Form
        {
            public const int WM_NCLBUTTONDOWN = 0xA1;
            public const int HT_CAPTION = 0x2;
            readonly ExploitAPI api = new ExploitAPI();
            private object file;
    
            public object ScriptBox { get; private set; }
    
            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
    
            private void FormMoveable_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            }
            public RoPloit()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                api.LaunchExploit();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                fastColoredTextBox1.Text = "";
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Txt Files (*.txt)|*.txt|Lua Files (*.lua)|*.lua|All Files (*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Stream s = sfd.OpenFile();
                    StreamWriter sw = new StreamWriter(s);
                    sw.Write(fastColoredTextBox1.Text);
                    sw.Close();
                }
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                //wassup
                OpenFileDialog opendialogfile = new OpenFileDialog();
                opendialogfile.Filter = "Lua File (*.lua)|*.lua|Text File (*.txt)|*.txt";
                opendialogfile.FilterIndex = 2;
                opendialogfile.RestoreDirectory = true;
                if (opendialogfile.ShowDialog() != DialogResult.OK)
                    return;
                try
                {
                    fastColoredTextBox1.Text = "";
                    System.IO.Stream stream;
                    if ((stream = opendialogfile.OpenFile()) == null)
                        return;
                    using (stream)
                        this.fastColoredTextBox1.Text = System.IO.File.ReadAllText(opendialogfile.FileName);
                }
                catch (Exception)
                {
                    int num = (int)MessageBox.Show("An unexpected error has occured", "OOF!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
                }
            }
    
            [Obsolete]
            private void button1_Click(object sender, EventArgs e)
            {
                string luac = fastColoredTextBox1.Text;
                api.SendLimitedLuaScript(luac);
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Minimized;
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
    
            }
        }
    }
    
    

    Monday, March 16, 2020 2:39 AM
  • here ya go https://1drv.ms/u/s!AtJ3ctEu5vlwgZ1IVtfgwrvf4Hq6rQ?e=5WnGYk
    Monday, March 16, 2020 3:28 AM
  • here ya go https://1drv.ms/u/s!AtJ3ctEu5vlwgZ1IVtfgwrvf4Hq6rQ?e=5WnGYk

    It looks to me that you are not even making an effort to even debug your program and find out what object is a null valued object,  and the line of code that is throwing the exception. You want someone to do it for you. You want someone to debug your program and fix it. You are clearly being told what the error message means and the cause of it.  

    This is something that you should simply be able to do on your own is find the error line and the object that not instanced.

    Monday, March 16, 2020 3:54 AM
  • well they asked for it so I give them it!
    Monday, March 16, 2020 3:56 AM
  • and it only says call stack or whatever not the problem
    Monday, March 16, 2020 3:58 AM
  • You are writing malware.  You can take your code and your question and flush it down the nearest toilet.  There's no way you should be playing around in that world if you aren't experienced enough to debug your own problems, because no one here is going to help you.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, March 16, 2020 4:03 AM
  • well they asked for it so I give them it!

    https://michaelscodingspot.com/debugging-part1/

    I suggest that you get busy.

    Monday, March 16, 2020 4:20 AM