0

I have written a very simple code in console application. I have created a static class i.e.

public static class A
{
 public static void GetData(Int16 Id)
 {
  //method code
 }
}

and I'm trying to call this method in main method i.e.

class Program
    {
        static void Main(string[] args)
        {
            A.GetData(21);
        }
    }

but I'm getting this error: "Object reference not set to an instance of an object."
Note: class "A" is in another project (within same solution) and I have added the reference of this project in startup (main) project. any solution please?

Hasnain Latif
  • 55
  • 1
  • 10

1 Answers1

3

There is nothing wrong with the code that you posted. If you're getting a null reference exception you're either:

  • Missing a using statement that points to where your static A class lives (you said that you've already included this.
  • Your GetData() method is throwing the null ref exception. This is the most likely scenario. A quick debugging session will answer the question for you.

I've created a quick and dirty .net fiddle to demonstrate that your code, at least what you posted, is working properly.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • Thank you so much for your answer, basically I'm getting multiple connection strings through configuration manager in class A (at class level) and putting them in string variable like this: static string CS1 = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString; static string CS2 = ConfigurationManager.ConnectionStrings["CS2"].ConnectionString; static string CS3 = ConfigurationManager.ConnectionStrings["CS3"].ConnectionString; when I remove this code then application works fine. – Hasnain Latif Apr 13 '20 at 11:47
  • 1
    That's gerat news, @HasnainLatif. I'm glad I was able to point you in the right direction! I noticed that you've never marked any answers as the correct/accepted answer for any of the questions you've asked. Marking an answer as the accepted answer is a great way of showing appreciation to all the folks that have helped you out. May I suggest that you go through all the questions you've asked and click the accept check when someone has answered your question? https://stackoverflow.com/help/someone-answers – James Hill Apr 13 '20 at 12:04
  • 1
    Oh, i never thought that people notice this thing. Thanks for telling me I really appreciate it and sure I'll vote from now. – Hasnain Latif Apr 13 '20 at 12:30
  • 1
    @HasnainLatif, I figured you didn't - no problem at all! Don't forget to click the check mark too! – James Hill Apr 13 '20 at 12:35
  • @HasnainLatif, just wanted to follow up. Take a look at this: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work, and go back through the questions you've asked. You're missing a step - accepting an answer. – James Hill May 13 '20 at 21:02