Variable is used before it has been assigned a value. A null reference exception could result at runtime.

"numstr"   is the input variable. It is set to receive data from another TextBox (TextBox9)which receives data from SQL Server.  
"newstr"  is the output variable. It is set to output to TextBox10.
TextBox9 is the numerical dollar amout on a check, while TextBox10 is the "word" dollar amount on a check. Just thought that info might help.

Why do you think I get this warning?
Warning      1      Variable 'newstr' is used before it has been assigned a value. A null reference exception could result at runtime.      

Some code below:

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 tempstr As String = ""


        numstr = TextBox9.Text
        TextBox10.Text = newstr



        If numstr >= 10 ^ 3 Then ' thousand            
            newstr = Int(numstr / 10 ^ 3)
        ElseIf newstr <> 0 Then
            tempstr = tempstr & newstr & "thousand "
        End If

jampostGeneral MaanagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
It's clear. You declare:
Dim newstr As String
... and nothing more, so your "newstr" variable has the value of Nothing.
2 lines of code after, you are doing this:
TextBox10.Text = newstr
So you're assigning the value Nothing to TextBox10.Text.

So, for what do you want "newstr"? Why do you set TextBox10.Text to newstr before assigning a value to the variable?

Hope that helps.
0
käµfm³d 👽Commented:
I'd also advice you against relying on implicit conversion as you are doing in the line:

newstr = Int(numstr / 10 ^ 3)

Open in new window


numstr is a String, and you are letting VB convert it to an Integer for you. What happens if the user enters "Hello World" in TextBox9? How would VB convert that to an Integer?

The safer thing to do would be to validate and convert the value. You can accomplish this in one step via Integer.TryParse:

Dim value As Integer

If Integer.TryParse(numstr, value) Then
    ' If you make it here, the value is a valid integer
    '  and the converted integer is stored inside of "value".
    '  Put your math inside of here, and use "value" instead
    '  "numstr" wherever you have a math operation
End If

Open in new window

0
jampostGeneral MaanagerAuthor Commented:
RolandDeschain,

 During run time, the variable newstr will pick up the Int part of the variable numstr to out put it to TextBox10 along with the word Thousand.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jampostGeneral MaanagerAuthor Commented:
Like this?

  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 = ""

        Dim value As Integer

        If Integer.TryParse(numstr, value) Then
            ' If you make it here, the value is a valid integer
            '  and the converted integer is stored inside of "value".
            '  Put your math inside of here, and use "value" instead
            '  "numstr" wherever you have a math operation
        End If



        numstr = TextBox9.Text
        TextBox10.Text = newstr

0
jampostGeneral MaanagerAuthor Commented:
RolandDeschain,

   There is one thing I should point out.  numstr is the net pay of a pay check; therefor it is unlikely that numstr will be an integer, and most likely a decimal.
0
jampostGeneral MaanagerAuthor Commented:
kaufmed,


If this statement (Below) permanently changes numster to an integer then it will be unusable for the purpose of paycheck printing. But if it is just for this line  --  newstr = Int(numstr / 10 ^ 3) which takes care of the 4th decimal place on the left then it is good after dividing by 10^3. My paycheck usually doesn't use that decimal place. The rest of the procedure takes care of the hundreds and everything else.

Dim value As Integer

If Integer.TryParse(numstr, value) Then
    ' If you make it here, the value is a valid integer
    '  and the converted integer is stored inside of "value".
    '  Put your math inside of here, and use "value" instead
    '  "numstr" wherever you have a math operation
End If


 If numstr >= 10 ^ 3 Then ' thousand            
            newstr = Int(numstr / 10 ^ 3)
0
jampostGeneral MaanagerAuthor Commented:
RolandDeschain,

I do have one question pertaning to posting  --  Warning      1      Variable 'newstr' is used before it has been assigned a value. A null reference exception could result at runtime.      

How do I assign--  TextBox10.Text = newstr   so that newstr is only used after it is asigned a value and therefore no longer null?
0
Anthony PerkinsCommented:
I agree the warning message is quite clear.
>> During run time, the variable newstr will pick up the Int part of the variable numstr to out put it to TextBox10 along with the word Thousand. <<
That code is not showm.  As it stands you are assigning Nothing to TextBox10.Text.
0
jampostGeneral MaanagerAuthor Commented:
acperkins,

   Now we are getting somewhere.  What you are saying is that this part of the code


            newstr = Int(numstr / 10 ^ 3)

is not saying  (or has some sort of problem)  newstr will pick up the Int part of the variable numstr (divided by 1,000)

and this    --  TextBox10.Text = newstr    I believe is saying to out put newstr (which is Int(numstr / 10 ^ 3)
)       to TextBox 10


    Can you tell me where I am off track with this code?






       
0
CodeCruiserCommented:
You can change

Dim newstr As String

to

Dim newstr As String=""

to get rid of the warning.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jampostGeneral MaanagerAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.