System.NullReferenceException was unhandled

jampost
jampost used Ask the Experts™
on
This particular error has been a real thorn in my side in different sreas of this program, but we'll just deal with this instance of it.  The code line that tripped the error is  "newstr = NumToWord(Int(numstr / 10 ^ 2))"

And the error is       --         System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
 .



   I did try inserting the key word NEW between As   and  String in the Declaration statement. But, got another error with that.




Private Sub NTW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NTW.Click

        Dim numstr As String
        Dim newstr As String
        Dim NumToWord As String
        Dim tempstr As String = ""


        numstr = TextBox9.Text
        If numstr = "0.00" Then
            newstr = "Zero"
            TextBox10.Text = NumToWord

 If numstr >= 10 ^ 2 Then

            newstr = NumToWord(Int(numstr / 10 ^ 2))
            numstr = ((numstr / 10 ^ 2) - Int(numstr / 10 ^ 2)) * 10 ^ 2

        ElseIf numstr = 0 Then
            tempstr = tempstr & newstr & "hundred "

        Else

            tempstr = tempstr & newstr & "hundred "

        End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Neil RussellTechnical Development Lead

Commented:
newstr = NumToWord(Int(numstr / 10 ^ 2))


You are taking the INT of a STRING devided by 10 ^2
numstr /10^2

Maybe? Its late and im tired :P
jampostGeneral Maanager

Author

Commented:
    Neilsr,

         Yes, That's exactly what the code is trying to do.  You're doing well in the PM.  I think Itellesence does not miss anything.  It's a blessing and a pain all wrapped up in one.
I assume that you have a function named "NumToWord" since you are passing a parameter to it.

The problem is you also have a local variable named NumToWord. Rename the local variable to something else to fix this.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

jampostGeneral Maanager

Author

Commented:
This was written as a function, but I am testing it inside of a click event named NTW.  
What is the use of the variable "NumToWord". Note that you never set the value for it which is causing the error.
jampostGeneral Maanager

Author

Commented:
       I got this code from another expert on Experts Exchange, so I'm just feeling my way around the code. I picked up on the same thing you have. It's like NumToWord is a fifth wheel.
        Isn't the keyword New suppose to fix problems like this? The variable is being used before a value is set. I know I am going to have to trim up this code, but I am just curiouse about this error.
The variable is being used before a value is set. I know I am going to have to trim up this code, but I am just curiouse about this error.

That is exactly the cause of the error. The variable was not set before it was used.

Can you explain what you are trying to do?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Can you post the full definition of NTW_Click? You've got multiple if blocks and only one is visibly ended, so it's difficult to tell how the code is structured.
jampostGeneral Maanager

Author

Commented:
I think this code has problems and I will work them out. I have the same error message on the variable "newstr", and newstr is an output variable. This is pretty complex code. I am going to work on it and bring it back another time. Thank you for your help.  
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
How is "newstr" an "output" variable? You are working in a Sub--there is no return value, and the parameters are dictated by the delegate signature.

If you give the overall goal, I'm sure one or more of us can help you reach it  = )
jampostGeneral Maanager

Author

Commented:
Thank you and happy New Year
jampostGeneral Maanager

Author

Commented:
Kaufmed,

     I have absolutly no doubt that you are capable of assisting me on this. You helped me a few times before.  This procedure was offered to me by an expert on this site. I think it has potential, but it has a lot of issues. Some of whitch you pointed out. I have decided to work on it and post my problems one at a time. I posted a new one just a couple of minutes ago.   Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial