locked
Exception error while compiling the web application RRS feed

  • Question

  • User662762443 posted

    Hello friends!

    When compiling my web application, I'm getting the following exception, returning logged-in user error:

    $ exception: {"Object reference not set to an instance of an object."} System.NullReferenceException

    Friday, July 27, 2018 1:38 PM

Answers

  • User-1924028965 posted

    Hi, 

    I can see the error happening in the following line.  when you use "as" keyword to cast an object, it returns null if "criteria.UniqueResult()" does not have an object where type is "User". 

    user = criteria.UniqueResult() as User;

    Therefore add a breakpoint and identify the content and the object type of the "criteria.UniqueResult()". 

    at the moment after hitting this line user object is assigned to null, therefore you will get the error what you getting now. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 8, 2018 10:32 AM

All replies

  • User1881638666 posted

    Hi rtaVix,

    Put a break point at the start of the try block of  public static User ConsultLogin(string login) method and debug the application.

    Then you could locate exact line where this error occurs. And hit F11 key and step into the calling methods to see what happened and

    the values / references set in the objects accessed in this call. Seems one the reference is not set to instance of that object when you run this block of code.

    By debugging the code you could locate and find. 

    Just go through the following links if you need assistance how to debug in VS.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    https://msdn.microsoft.com/en-us/library/sc65sadd.aspx

    Thanks

    Wenushka

    Saturday, July 28, 2018 3:55 AM
  • User-1003897979 posted

    Delete the try and catch block.  It doesn't do anything except hide the error you are trying to track down.  Once you delete those the exception report will tell you which line has the null variable.

    Then use the debugger to work out why the variable you think should have a value actually doesn't.

    (PS - having try/catch blocks like this is always bad.  It swallows the actual exception and raises a new one at the 'throw' statement.  Both silly and useless.)

    Saturday, July 28, 2018 4:31 AM
  • User283571144 posted

    Hi rtaVix,

    Object reference not set to an instance of an object. exactly what it says, you are trying to use a null object as if it was a properly referenced object. Most of the time, when you try to assing value into object, and if the value is null, then this kind of exception occur.

    I suggest you could set a breakpoint at the codes to find out which line throw the error.

    Besides, I have create a test demo according to you codes, it works well.

    I suggest you could post more details about the user class and which line throw the error.

    Best Regards,

    Brando

    Tuesday, July 31, 2018 5:09 AM
  • User-1924028965 posted

    Hi, 

    I can see the error happening in the following line.  when you use "as" keyword to cast an object, it returns null if "criteria.UniqueResult()" does not have an object where type is "User". 

    user = criteria.UniqueResult() as User;

    Therefore add a breakpoint and identify the content and the object type of the "criteria.UniqueResult()". 

    at the moment after hitting this line user object is assigned to null, therefore you will get the error what you getting now. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 8, 2018 10:32 AM