VB.Net – Compilation Warning – “Variable is used before it has been assigned a value. A Null Reference could result at runtime”

Variables

Looking at an aged code and I started rearranging it a bit.

Unfortunately, re-arranged, it stopped working.

I took a night’s sleep.

Come the next day, less tired and wearied, I went back at it.

Thankfully, I had made a backup and had it tucked away.

Looking at the saved code, I found out that the original code had set a couple of variables to values other than their default values.

 

Compiler Warnings

Once I arrived back at a working code, it was time to clear the compiler warnings.

The specific compiler warning that will be addressed in this post reads:

Textual

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

 

Image

Code

Original

Code


Imports System
				
Public Module Module1
	
	Public Sub Main()

		Call handleStringArray
		
	End Sub
	
		
	private Function handleStringArray()

		Dim stringArray() as String
		Dim iNumberofElements as Integer
		
		Try

			For i = 0 to 100

				'ReDim Preserve stringArray(i)

			Next
		
			iNumberofElements = stringArray.length
		

		Catch ex As Exception
		
			Console.WriteLine("Exception :- " + ex.Message)
		
		End Try
	
		return ( iNumberofElements )

	End Function
		
End Module

 

Revision

Outline

  1. Declaration without initialization
    • Dim stringArray() as String
  2. Declarations with initialization
    • Dim stringArray(-1) as String
    • Dim stringArray() as String = New String(-1) {}

 

Code


Imports System
				
Public Module Module1
	
	Public Sub Main()

		Call handleStringArray
		
	End Sub
	
		
	private Function handleStringArray()

		'Dim stringArray() as String
		Dim stringArray(-1) as String 
		Dim iNumberofElements as Integer
		
		Try

			For i = 0 to 100

				'ReDim Preserve stringArray(i)

			Next
		
			iNumberofElements = stringArray.length
		

		Catch ex As Exception
		
			Console.WriteLine("Exception :- " + ex.Message)
		
		End Try
	
		return ( iNumberofElements )

	End Function
		
End Module

Special Recognition

.Net Perls

Array Vb.Net ( Link ) and Vb.Net String Constructor ( Link )

  1. Sam Allen

 

Stack Overflow

Link

Commendation

  1. Bjørn-Roger Kringsjå
    • Turn Option Strict On
      • Force you from creating functions like Function MyArray() As Surprise, and functions that should have been a Sub
  2. Meta-Knight
    • Don’t surround your code with an empty try-catch

Image

 

References

  1. DotnetPerls
    • VB.NET Array Examples, String ArraysCreate
      Link
    • VB.NET String Constructor (New String)
      Link
  2. StackOverflow
    • Array variable is used before assigned a value
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s