Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have been trying this code and the weird thing is that it works on WIN7 x64 but not on winXP x32.

C#
public static void getip()
      {
          try
          {
              var vpn = NetworkInterface.GetAllNetworkInterfaces()
                                            .FirstOrDefault(x => x.Description == "TAP-Win32 Adapter V9");
              var ip = vpn.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Address.ToString();
              if (ip.ToString().StartsWith("169.254.") || (ip.ToString() == string.Empty))
              {
                  vpnIP = "No_VPN";
                  Console.WriteLine(vpnIP);
                  Console.ReadLine();


              }
              else
              {
                  vpnIP = ip.ToString();

                  Console.WriteLine(vpnIP);
                  Console.ReadLine();

              }
          }
          catch (Exception ex)
          {
              using (StreamWriter writer = new StreamWriter("c:\\dp\\vpn.txt"))
              {
                  writer.Write(ex);
              }
          }
      }


When I run it on xp x32 i'm writing the exception out to a text document and it says.
"Object reference not set to an instance of an object."

Ideas anyone?

I don't have to use LINQ if someone knows a better way i just need to get the ip address of a particular adapter.

Thanks in Advance!
Posted
Comments
Sergey Alexandrovich Kryukov 11-Oct-13 17:16pm    
In what line?
—SA

vpn may be null on the XP box. FirstOrDefault... returns "default" if not found, which is null for reference types.
You de-reference vpn unchecked by var ip = vpn.GetIPProperties...
Cheers
Andi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Oct-13 17:22pm    
Well spotted, a 5. In my answer, I spotted another, unrelated problem, and provided further advice, please see.
—SA
Andreas Gieriet 11-Oct-13 17:28pm    
Hello Sergey,
Thanks for your 5!
Cheers
Andi
As Andi pointed out in Solution 1, the return of FirstOrDefaulr could give you null. The problem is: you did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Another, unrelated problem is the path "c:\dp\vpn.txt". Using such path in Windows is not good, and in Windows 7 or later it is just illegal. What should you use instead? Please see my past answers:
How to find my programs directory (executable directory),
How to find my programs directory (current directory, "special folders");
which is better : use of registery in order to store setting or use of Settings class[^].

And, finally, there are no situations where a hard-coded path, relative or absolute, can be useful. Path names should be calculated during runtime, based on some system- and account-defined directories as shown above, taken from user input, some configuration files, and so on.

Good luck,
—SA
 
Share this answer
 
v2
Comments
Andreas Gieriet 11-Oct-13 17:27pm    
Hello Sergey,
As always: very precise an useful information!
My 5!
Cheers
Andi
Sergey Alexandrovich Kryukov 11-Oct-13 17:38pm    
Thank you very much, Andi.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900