0

Here is my scenario: I am trying to get a value from frontend using AJAX and then want to use this in a non-static method for some calculations. But I am getting an error:

Object reference not set to an instance of an object

I know how to use non-static method/data members in a static method by creating obrect Reference that I did but still error is same.

Code:

[WebMethod]
[WebScript]
public static string refAssignments(getVal rf)
{
    string value = rf.valueFromAJAX;
    MyClass obj = new MyClass();

    string result = obj.analyse(value);
}

Here Analyse() is a non-static method all I want is to use this method inside the static method refAssignments.

I tried earlier questions but wasn't successful! Can someone point where I am doing wrong

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mist Umar
  • 3
  • 1
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sir Rufo Dec 16 '17 at 08:52
  • @SirRufo can you provide solution? I am confused in that question I have read but failed to achieve in my Scenario – Mist Umar Dec 16 '17 at 08:54
  • Well, read the duplicate question and the answers. You try to use a reference when the reference points to null. Don't do that or ensure that you have a valid reference. Thats all – Sir Rufo Dec 16 '17 at 08:58
  • is there anyway to delete my question ? :-D i m dead what i was doing is totally NERD thing – Mist Umar Dec 16 '17 at 09:06
  • go to the [help center](https://stackoverflow.com/help) and read what you can do and what you can not do here on stackoverflow. It is documented and waiting for you – Sir Rufo Dec 16 '17 at 09:26

1 Answers1

1

The problem isn't relevant with the Analyse method is non-static or not. Probably, rf object is null. Check the rf object;

if (rf != null)
{
    string value = rf.valueFromAJAX;
    MyClass obj = new MyClass();
    string result = obj.analyse(value);
}
else
{
   //Do something
}
lucky
  • 12,734
  • 4
  • 24
  • 46