You must Sign In to post a response.
  • Category: .NET

    Object reference is not set to instance .

    I got above error when my program runs. It gives error at SendKeys.SendWait method.
    It gets proper input but didn't work. Actually in my scenario I want to send received data to activated window (i.e word , excel , notepad , or any application text box which is activated at that instance).
    Help me please, Thanks

    here is code,

    Hide Copy Code
    if (res.StartsWith("*"))
    {
    SendKeys.SendWait(res.ToString());
    }

    In above code "res" is string received from socket and i want to send this string to activated window i.e text editor opened at runtime.
  • #761026
    Hi

    Object reference is not set to instance
    error Mostly your object is nothing instance there
    so you first find object is correct or not then values coming or not then check them

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761028
    I checked value is coming in "res" variable. but it doesnt send data to file

  • #761029
    Hello Priyanka,

    By providing this much code we can not help you at the end. you should provide us that much of code where we can help you at least.

    The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler.
    catch(Exception ex)
    {
    MessageBox.Show(ex.StackTrace);
    }

    Mostly this error occurs due to your object doesn't have instance. I suggest you to put a breakpoint in your code and try to debug it and check whether object has an instance or not.

    You can visit the MSDN Library for NullReferenceException on below link :

    https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761031
    void sendkeytest(string input)
    {
    //static extern int SetForegroundWindow(IntPtr point);
    Process p = Process.GetCurrentProcess();
    IntPtr h = p.MainWindowHandle;
    SetForegroundWindow(h);

    if (p.Responding)
    {
    foreach (char c in input)
    {
    SendKeys.SendWait(c.ToString());
    }
    }
    }

    if (res.StartsWith("*"))
    {
    sendkeytest(res);
    }

  • #761185
    Hi,

    Check the null case before assign value like below.


    if (res !=string.Empty && res.StartsWith("*"))
    {
    SendKeys.SendWait(res.ToString());
    }


    hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments