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

jampost
jampost used Ask the Experts™
on
"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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Luis PérezSoftware Architect in .Net

Commented:
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.
ǩ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:
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

jampostGeneral Maanager

Author

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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

jampostGeneral Maanager

Author

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

jampostGeneral Maanager

Author

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.
jampostGeneral Maanager

Author

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)
jampostGeneral Maanager

Author

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?
Top Expert 2012

Commented:
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.
jampostGeneral Maanager

Author

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?






       
Most Valuable Expert 2012
Top Expert 2014
Commented:
You can change

Dim newstr As String

to

Dim newstr As String=""

to get rid of the warning.
jampostGeneral Maanager

Author

Commented:
Thank you

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