NullReferenceException while calling the method using typename RRS feed

  • Question

  • Hi,

    I had created the C# application,It has the two cs files




    file1.cs contains the copy method, it declared as static

    i call the static method in the file2.cs like


    while execute it NullReferenceException occurred

    what to do for this problem?

    can anyone answer for this question?

    Monday, June 20, 2011 6:17 AM


  • Hi,

    please provide more information about the code. Without any details what you are doing, we cannot help you.

    All I can tell you now is:

    The names of the files does not matter at all. But it is a good practice to keep the filename the same as the name of the class that is defined inside the file and only have one class inside the file.

    When you execute your software from Visual Studio in Debug mode, then you will be able to see the exact line where the exception occured and all exception details. And you can examine the content of all local variables so you can see, where a reference is null.

    So maybe you want to give us the code of your copy method inside the file1 class together with the information where the null reference exception occured.

    A last hint that might be helpfull: It could be a good idea to read a little bit about coding standards / naming conventions. that is nothing that must be done but it could help reading code. And once familar with it, it makes it much easier even for yourself. (And it isnothing hard. It would change your file1 class to File1 - maybe with a better naming, because what is file1? FileHelper or soemthing like that could be name with helper functions regarding files ... and a public method would be named Copy instead of copy.) But that is just a small hint - nothing you have to do at all. You will always be welcome to the forum with any way you write your code!

    And some general help with such an exception:

    a) Avoid calling-chains like "SomeSobject.SomeFunction().AnotherFUnction().AndAgainAFunction();". You cannot exactly say, where the null reference exception occured - Someobject could be null or the result of SomeFunction or AnotherFunction could have been null!

    b) When defining a reference (all Classes are reference types), you will get a null reference first. So before calling any method, you have to assign a value to it (e.g. MyClass class1 = new MyClass(); )

    Sorry, that I was unable to help you in a better way. If you give more details, I will try to help more.

    With kind regards,


    • Marked as answer by Lie You Monday, June 27, 2011 1:55 AM
    Monday, June 20, 2011 6:45 AM