Link to home
Start Free TrialLog in
Avatar of jampost
jampostFlag for United States of America

asked on

System.NullReferenceException was unhandled

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
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of jampost

ASKER

    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.
Avatar of jampost

ASKER

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.
Avatar of jampost

ASKER

       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.
ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jampost

ASKER

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.  
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  = )
Avatar of jampost

ASKER

Thank you and happy New Year
Avatar of jampost

ASKER

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.