locked
Object reference not set to an instance of an object. RRS feed

  • Question

  • Hello there,

    I am having an error with my code.

    Here is a screenshot: 

     public bool IsSessionConnected
            {
                get
                {
                    return this.state == SessionState.Connected;
                }
            }
    
            private void btnScanConsole_Click(object sender, RoutedEventArgs e)
            {
                consoleDiscoveryManager = null;
                List<XboxConsole> list = new List<XboxConsole>();
                consoleDiscoveryManager.StartConsoleDiscovery();
                consoleDiscoveryManager = null;
            }
    
            private void btnCheck_Click(object sender, RoutedEventArgs e)
            {
                if (this.IsSessionConnected)
                {
                    txtConsoleList.Text = "Connected to:" + xboxConsole.DisplayName + "\r\n";
                }
                else
                {
                    txtConsoleList.Text = "No connection\r\n";
                }
            }

    This is throwing me the object reference not set to an instance of an object

    consoleDiscoveryManager.StartConsoleDiscovery();
    

    Anyone have a solution?

    Thursday, February 13, 2014 8:13 PM

Answers

  • No, I do not know how to set a debugger breakpoint or single-step.

    So you are just stabbing at thin air, huh? :)

    Debugging code is an essential part of coding. I suggest that you figure out how to debug code with the VS Debugger. 

    http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

    IConsoleDiscoveryManagerconsoleDiscMngr = new IConsoleDiscoveryManager

    No the above you cannot do because the name starts with an "I" that signifies that it is an Interface. The error message is telling you that you can't make it an instance of object.

    At best, you can do this at the top of your class.

    public IConsoleDiscoveryManager consoleDiscMngr {get; set;}

    Then is some method where you are trying to use consoleDiscMngr, you can do this.

    consoleDiscMngr = new ConsoleDiscoveryManager();

    IConsoleDiscoveryManager the interface is used by the class called ConsoleDiscoveryManager.

    Now that consoleDiscMngr is an instance of ConsoleDiscoveryManager you can do this

    var data = consoleDiscMngr.GetData();   // that's if ConsoleDiscoveryManager had a public method in it called GetData().

    Take note on this, IConsoleDiscoveryManager would have a definition for GetData() that points to the public GetData() that is ConsoleDiscoveryManager class.

    Forget about the Interface and just do this don't even use it.

    var consoleDiscMngr = new ConsoleDiscoveryManager();

    var somedata = consoleDiscMngr.GetData();

    or

    var somedata = new ConsoleDiscoveryManager().GetData();

    Note: "var" can only be used inside of a method(). Look up "var" in C# .NET and find out what it means.

    • Marked as answer by Eason_H Monday, February 24, 2014 3:32 AM
    Thursday, February 13, 2014 10:00 PM
  • Your problem is that you don't understand C# essentials.

    Object reference not set to an instance of an object

    That means that the object (or variable if it is more likely for you to understand) is not instantiate. When you want to create a new object from some class that means that you have to make an instance of an object like this:

    SomeClass someClass = new SomeClass();

    which means that you have reserve the place in the memory for object someClass and associate the properties, methods, events,... of class SomeClass to that object which you can use by using object name someClass. Of course, you will also have to understand what is static class.

    Cannot create an instance of the abstract class or interface

    This means that you can't create an instance from an interface. Why is that? Because interface only contains definitions of methods, not their implementations, so how could any compiler understand what are you assoicating to some object.

    Sorry, but those thigs are the very basic things that you MUST understand before you code your own program.


    Development Technician, Microsoft Certified Professional

    • Proposed as answer by Ramya SJ Thursday, February 20, 2014 1:46 PM
    • Marked as answer by Eason_H Monday, February 24, 2014 3:32 AM
    Saturday, February 15, 2014 12:46 AM

All replies

  • you are setting consoleDiscoveryManager to null two lines before the call to StartConsoleDiscovery...

    What is the type of consoleDiscoveryManager? You have to initialize it somewhere before the call to StartConsoleDiscovery, something like

    consoleDiscoveryManager = new ConsoleDiscoveryManager();


    https://fraktalysator.codeplex.com


    • Edited by ArgusMagnus Thursday, February 13, 2014 8:30 PM
    Thursday, February 13, 2014 8:28 PM
  • Even without that it gives me the same error.
    Thursday, February 13, 2014 8:29 PM
  • I declared them like this:
     public XboxConsole xboxConsole;
            public IConnectionManager connectionManager;
            private IConsoleDiscoveryManager consoleDiscoveryManager;
            public XboxApp xboxApp;
            private SessionState state;
            private Dictionary<string, XboxConsole> consoles = new Dictionary<string, XboxConsole>();
    

    Thursday, February 13, 2014 8:31 PM
  • Even without that it gives me the same error.

    The object must exist in memory, an instance.  You can't reference the object in code if the object was never created. Why the object is not created by the time your code addresses it, you are going to have to debug the code.

    Object Oriented Programming is what you must understand based on the link. And it doesn't matter if it's .NET or JAVA, because OOP is OOP and the principles are the same in an OOP language. 

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Basically it is this.

    MrBilleh theobject = new MrBilleh();  //The object now exist in memory.

    You try to address theobject in code without the above taking place, then it's BAM 'object not set to an instance of an object'. The object has a null value, and it does not exist. 

    Thursday, February 13, 2014 8:44 PM
  • Thanks for trying to help but I have tried to address it again with what you guys suggested but it throws me another error. Here is the full code of the application.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Globalization;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using SmartGlass.Common.Contracts;
    using SmartGlass.Common.Model;
    using Toolkit.Common;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace XODebugWin8
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public XboxConsole xboxConsole;
            public IConnectionManager connectionManager;
            public XboxApp xboxApp;
            private SessionState state;
            private Dictionary<string, XboxConsole> consoles = new Dictionary<string, XboxConsole>();
            public IConsoleDiscoveryManager consoleDiscoveryManager; // Suggested by darnold
    
            public MainPage()
            {
                InitializeComponent();
            }
    
    
            private void btnConnect_Click(object sender, RoutedEventArgs e)
            {
    
            }
            public bool IsSessionConnected
            {
                get
                {
                    return this.state == SessionState.Connected;
                }
            }
    
            private void btnScanConsole_Click(object sender, RoutedEventArgs e)
            {
                
                consoleDiscoveryManager.StartConsoleDiscovery(); // Object error
            }
    
            private void btnCheck_Click(object sender, RoutedEventArgs e)
            {
                if (this.IsSessionConnected)
                {
                    txtConsoleList.Text = "Connected to:" + xboxConsole.DisplayName + "\r\n";
                }
                else
                {
                    txtConsoleList.Text = "No connection\r\n";
                }
            }
        }
    }
    


    Thursday, February 13, 2014 8:53 PM
  • Thanks for trying to help but I have tried to address it again with what you guys suggested but it throws me another error. Here is the full code of the application.

    And what error message would that be?

    And do you know how to set a debugger breakpoint and single-step until you hit the line that blows up?

    Do you know what object had a null value and the line that through the error?

    You can hover over the line with an object using the mouse pointer before you execute the line in a single-step debug process,  and VS will tell you if the object is null or not null.

    Thursday, February 13, 2014 9:08 PM
  • No, I do not know how to set a debugger breakpoint or single-step.

    The error when I try and set the object:

    IConsoleDiscoveryManager consoleDiscMngr = new IConsoleDiscoveryManager();

    "Cannot create an instance of the abstract class or interface" ^


    Thursday, February 13, 2014 9:16 PM
  • No, I do not know how to set a debugger breakpoint or single-step.

    So you are just stabbing at thin air, huh? :)

    Debugging code is an essential part of coding. I suggest that you figure out how to debug code with the VS Debugger. 

    http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

    IConsoleDiscoveryManagerconsoleDiscMngr = new IConsoleDiscoveryManager

    No the above you cannot do because the name starts with an "I" that signifies that it is an Interface. The error message is telling you that you can't make it an instance of object.

    At best, you can do this at the top of your class.

    public IConsoleDiscoveryManager consoleDiscMngr {get; set;}

    Then is some method where you are trying to use consoleDiscMngr, you can do this.

    consoleDiscMngr = new ConsoleDiscoveryManager();

    IConsoleDiscoveryManager the interface is used by the class called ConsoleDiscoveryManager.

    Now that consoleDiscMngr is an instance of ConsoleDiscoveryManager you can do this

    var data = consoleDiscMngr.GetData();   // that's if ConsoleDiscoveryManager had a public method in it called GetData().

    Take note on this, IConsoleDiscoveryManager would have a definition for GetData() that points to the public GetData() that is ConsoleDiscoveryManager class.

    Forget about the Interface and just do this don't even use it.

    var consoleDiscMngr = new ConsoleDiscoveryManager();

    var somedata = consoleDiscMngr.GetData();

    or

    var somedata = new ConsoleDiscoveryManager().GetData();

    Note: "var" can only be used inside of a method(). Look up "var" in C# .NET and find out what it means.

    • Marked as answer by Eason_H Monday, February 24, 2014 3:32 AM
    Thursday, February 13, 2014 10:00 PM
  • From what library/namespace is IConsoleDiscoveryManager? Don't you have a documentation for this library that you are using? It may contain examples on how you can create an instance.

    https://fraktalysator.codeplex.com

    Friday, February 14, 2014 10:37 AM
  • Your problem is that you don't understand C# essentials.

    Object reference not set to an instance of an object

    That means that the object (or variable if it is more likely for you to understand) is not instantiate. When you want to create a new object from some class that means that you have to make an instance of an object like this:

    SomeClass someClass = new SomeClass();

    which means that you have reserve the place in the memory for object someClass and associate the properties, methods, events,... of class SomeClass to that object which you can use by using object name someClass. Of course, you will also have to understand what is static class.

    Cannot create an instance of the abstract class or interface

    This means that you can't create an instance from an interface. Why is that? Because interface only contains definitions of methods, not their implementations, so how could any compiler understand what are you assoicating to some object.

    Sorry, but those thigs are the very basic things that you MUST understand before you code your own program.


    Development Technician, Microsoft Certified Professional

    • Proposed as answer by Ramya SJ Thursday, February 20, 2014 1:46 PM
    • Marked as answer by Eason_H Monday, February 24, 2014 3:32 AM
    Saturday, February 15, 2014 12:46 AM