In Reference to Search Utilities What Does an Index Do
NullReference Exception — Visual Basic
The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the aforementioned exception defined in the .Net Framework which they both use. Causes unique to Visual Basic are rare (perhaps but one).
This answer will use Visual Basic terms, syntax, and context. The examples used come up from a large number of past Stack Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A chip more explanation is likewise provided for those who might need it. An example similar to yours is very likely listed here.
Note:
- This is concept-based: there is no lawmaking for you to paste into your project. It is intended to help you understand what causes a
NullReferenceException(NRE), how to find it, how to set it, and how to avert it. An NRE tin can be caused many ways so this is unlikely to exist your sole run across. - The examples (from Stack Overflow posts) do not always show the all-time way to practise something in the first place.
- Typically, the simplest remedy is used.
Bones Significant
The message "Object not set to an example of Object" means you are trying to use an object which has not been initialized. This boils down to 1 of these:
- Your lawmaking declared an object variable, but it did not initialize information technology (create an instance or 'instantiate' it)
- Something which your lawmaking assumed would initialize an object, did non
- Possibly, other code prematurely invalidated an object withal in use
Finding The Crusade
Since the problem is an object reference which is Nothing, the answer is to examine them to find out which 1. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values - the culprit volition be Cypher.
You lot should also remove any Endeavor/Take hold of blocks from the relevant code, peculiarly ones where there is zilch in the Catch block. This will cause your code to crash when it tries to use an object which is Null. This is what you lot want because information technology will identify the verbal location of the problem, and let you to identify the object causing it.
A MsgBox in the Catch which displays Mistake while... will be of little help. This method likewise leads to very bad Stack Overflow questions, because you tin't describe the actual exception, the object involved or even the line of code where information technology happens.
You can also use the Locals Window (Debug -> Windows -> Locals) to examine your objects.
Once you lot know what and where the problem is, it is commonly fairly like shooting fish in a barrel to fix and faster than posting a new question.
See as well:
- Breakpoints
- MSDN: How to: Use the Try/Take hold of Block to Catch Exceptions
- MSDN: Best Practices for Exceptions
Examples and Remedies
Class Objects / Creating an Example
Dim reg As CashRegister ... TextBox1.Text = reg.Corporeality ' NRE The problem is that Dim does not create a CashRegister object; information technology only declares a variable named reg of that Blazon. Declaring an object variable and creating an example are two different things.
Remedy
The New operator tin can often be used to create the instance when you declare it:
Dim reg As New CashRegister ' [New] creates instance, invokes the constructor ' Longer, more explicit form: Dim reg As CashRegister = New CashRegister When it is only appropriate to create the instance later:
Individual reg As CashRegister ' Declare ... reg = New CashRegister() ' Create instance Annotation: Do not use Dim again in a procedure, including the constructor (Sub New):
Private reg As CashRegister '... Public Sub New() '... Dim reg Equally New CashRegister Finish Sub This volition create a local variable, reg, which exists just in that context (sub). The reg variable with module level Scope which you lot volition apply everywhere else remains Null.
Missing the
Newoperator is the #1 crusade ofNullReference Exceptionsseen in the Stack Overflow questions reviewed.Visual Basic tries to make the procedure clear repeatedly using
New: Using theNewOperator creates a new object and callsSub New-- the constructor -- where your object can perform any other initialization.
To be clear, Dim (or Private) only declares a variable and its Type. The Scope of the variable - whether it exists for the entire module/class or is local to a procedure - is determined by where it is declared. Private | Friend | Public defines the access level, non Scope.
For more information, see:
- New Operator
- Scope in Visual Basic
- Access Levels in Visual Basic
- Value Types and Reference Types
Arrays
Arrays must besides be instantiated:
Individual arr as Cord() This array has only been declared, not created. There are several ways to initialize an array:
Private arr as String() = New Cord(ten){} ' or Private arr() As String = New String(ten){} ' For a local assortment (in a procedure) and using 'Pick Infer': Dim arr = New String(x) {} Notation: Get-go with VS 2010, when initializing a local array using a literal and Option Infer, the As <Type> and New elements are optional:
Dim myDbl As Double() = {ane.5, 2, 9.9, 18, three.14} Dim myDbl = New Double() {i.5, 2, 9.9, 18, iii.14} Dim myDbl() = {1.five, 2, 9.9, xviii, iii.14} The data Type and array size are inferred from the information being assigned. Class/Module level declarations still require As <Type> with Option Strict:
Private myDoubles Every bit Double() = {i.five, 2, nine.nine, xviii, 3.fourteen} Example: Array of class objects
Dim arrFoo(5) Every bit Foo For i As Integer = 0 To arrFoo.Count - 1 arrFoo(i).Bar = i * 10 ' Exception Side by side The array has been created, just the Foo objects in it have non.
Remedy
For i As Integer = 0 To arrFoo.Count - 1 arrFoo(i) = New Foo() ' Create Foo instance arrFoo(i).Bar = i * x Next Using a List(Of T) will make information technology quite difficult to have an chemical element without a valid object:
Dim FooList Equally New List(Of Foo) ' List created, but it is empty Dim f As Foo ' Temporary variable for the loop For i As Integer = 0 To 5 f = New Foo() ' Foo case created f.Bar = i * ten FooList.Add together(f) ' Foo object added to listing Next For more information, see:
- Selection Infer Statement
- Scope in Visual Basic
- Arrays in Visual Basic
Lists and Collections
.NET collections (of which there are many varieties - Lists, Dictionary, etc.) must besides be instantiated or created.
Private myList As List(Of Cord) .. myList.Add("ziggy") ' NullReference You lot get the same exception for the same reason - myList was only declared, but no example created. The remedy is the same:
myList = New Listing(Of String) ' Or create an example when declared: Private myList Every bit New List(Of Cord) A common oversight is a class which uses a collection Blazon:
Public Class Foo Private barList As List(Of Bar) Friend Function BarCount As Integer Return barList.Count Cease Function Friend Sub AddItem(newBar As Bar) If barList.Contains(newBar) = False Then barList.Add(newBar) End If End Function Either procedure will result in an NRE, because barList is but declared, non instantiated. Creating an instance of Foo volition non as well create an instance of the internal barList. It may accept been the intent to exercise this in the constructor:
Public Sub New ' Constructor ' Stuff to practise when a new Foo is created... barList = New List(Of Bar) End Sub As before, this is incorrect:
Public Sub New() ' Creates another barList local to this process Dim barList As New List(Of Bar) End Sub For more information, see List(Of T) Class.
Data Provider Objects
Working with databases presents many opportunities for a NullReference because there tin can be many objects (Command, Connection, Transaction, Dataset, DataTable, DataRows....) in use at once. Note: Information technology does not matter which data provider yous are using -- MySQL, SQL Server, OleDB, etc. -- the concepts are the same.
Instance 1
Dim da As OleDbDataAdapter Dim ds As DataSet Dim MaxRows As Integer con.Open up() Dim sql = "SELECT * FROM tblfoobar_List" da = New OleDbDataAdapter(sql, con) da.Fill(ds, "foobar") con.Shut() MaxRows = ds.Tables("foobar").Rows.Count ' Error As before, the ds Dataset object was declared, just an instance was never created. The DataAdapter will fill up an existing DataSet, non create one. In this case, since ds is a local variable, the IDE warns you that this might happen:
When declared as a module/grade level variable, equally appears to be the case with con, the compiler tin't know if the object was created by an upstream process. Do not ignore warnings.
Remedy
Dim ds As New DataSet Example 2
ds = New DataSet da = New OleDBDataAdapter(sql, con) da.Fill(ds, "Employees") txtID.Text = ds.Tables("Employee").Rows(0).Item(1) txtID.Proper name = ds.Tables("Employee").Rows(0).Item(2) A typo is a trouble hither: Employees vs Employee. In that location was no DataTable named "Employee" created, so a NullReferenceException results trying to admission it. Another potential trouble is assuming there volition be Items which may not be so when the SQL includes a WHERE clause.
Remedy
Since this uses one table, using Tables(0) will avoid spelling errors. Examining Rows.Count can too help:
If ds.Tables(0).Rows.Count > 0 Then txtID.Text = ds.Tables(0).Rows(0).Item(one) txtID.Name = ds.Tables(0).Rows(0).Item(2) Cease If Make full is a function returning the number of Rows affected which can also be tested:
If da.Make full(ds, "Employees") > 0 Then... Instance 3
Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO, TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN FLIGHT_DETAILS Equally Flying ... WHERE [TICKET.TICKET_NO]= ...", con) Dim ds Equally New DataSet da.Make full(ds) If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then The DataAdapter will provide TableNames as shown in the previous case, just it does not parse names from the SQL or database table. As a result, ds.Tables("TICKET_RESERVATION") references a non-real tabular array.
The Remedy is the same, reference the table by index:
If ds.Tables(0).Rows.Count > 0 And then See too DataTable Class.
Object Paths / Nested
If myFoo.Bar.Items IsNot Cypher Then ... The code is only testing Items while both myFoo and Bar may too be Nothing. The remedy is to test the entire chain or path of objects 1 at a time:
If (myFoo IsNot Nothing) AndAlso (myFoo.Bar IsNot Nothing) AndAlso (myFoo.Bar.Items IsNot Nothing) Then .... AndAlso is of import. Subsequent tests will non be performed in one case the first False condition is encountered. This allows the code to safely 'drill' into the object(southward) one 'level' at a time, evaluating myFoo.Bar simply subsequently (and if) myFoo is determined to be valid. Object chains or paths tin get quite long when coding complex objects:
myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename") It is not possible to reference anything 'downstream' of a nil object. This also applies to controls:
myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value" Here, myWebBrowser or Document could be Goose egg or the formfld1 element may non exist.
UI Controls
Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _ & "FROM Invoice where invoice_no = '" & _ Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _ Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _ Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _ Me.expiry.Text & "'", con) Among other things, this lawmaking does non anticipate that the user may not accept selected something in 1 or more UI controls. ListBox1.SelectedItem may well be Nothing, so ListBox1.SelectedItem.ToString will outcome in an NRE.
Remedy
Validate information before using it (also use Choice Strict and SQL parameters):
Dim decease Every bit DateTime ' for text appointment validation If (ComboBox5.SelectedItems.Count > 0) AndAlso (ListBox1.SelectedItems.Count > 0) AndAlso (ComboBox2.SelectedItems.Count > 0) AndAlso (DateTime.TryParse(decease.Text, death) And then '... do stuff Else MessageBox.Prove(...error message...) End If Alternatively, you can utilize (ComboBox5.SelectedItem IsNot Cypher) AndAlso...
Visual Basic Forms
Public Class Form1 Private NameBoxes = New TextBox(v) {Controls("TextBox1"), _ Controls("TextBox2"), Controls("TextBox3"), _ Controls("TextBox4"), Controls("TextBox5"), _ Controls("TextBox6")} ' same thing in a unlike format: Private boxList Equally New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...} ' Immediate NRE: Private somevar As String = Me.Controls("TextBox1").Text This is a fairly common fashion to get an NRE. In C#, depending on how it is coded, the IDE will report that Controls does not exist in the current context, or "cannot reference non-static member". So, to some extent, this is a VB-only situation. It is as well circuitous because it tin result in a failure cascade.
The arrays and collections cannot be initialized this mode. This initialization code volition run before the constructor creates the Form or the Controls. As a outcome:
- Lists and Drove will just be empty
- The Assortment will comprise v elements of Nothing
- The
somevarassignment will consequence in an firsthand NRE considering Nix doesn't have a.Textproperty
Referencing assortment elements later will result in an NRE. If you do this in Form_Load, due to an odd issues, the IDE may not report the exception when information technology happens. The exception will pop up later when your lawmaking tries to utilise the array. This "silent exception" is detailed in this mail. For our purposes, the fundamental is that when something catastrophic happens while creating a form (Sub New or Form Load event), exceptions may go unreported, the code exits the procedure and but displays the class.
Since no other code in your Sub New or Class Load result will run afterwards the NRE, a great many other things can be left uninitialized.
Sub Form_Load(..._ '... Dim proper noun As String = NameBoxes(ii).Text ' NRE ' ... ' More lawmaking (which will probable non be executed) ' ... Stop Sub Note this applies to whatsoever and all control and component references making these illegal where they are:
Public Class Form1 Private myFiles() As Cord = Me.OpenFileDialog1.FileName & ... Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..." Individual studentName As Cord = TextBox13.Text Partial Remedy
It is curious that VB does not provide a warning, but the remedy is to declare the containers at the class level, only initialize them in form load event handler when the controls do exist. This tin be done in Sub New as long as your code is subsequently the InitializeComponent call:
' Module level declaration Private NameBoxes as TextBox() Individual studentName Every bit String ' Form Load, Form Shown or Sub New: ' ' Using the OP'southward approach (illegal using Selection STRICT) NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...) studentName = TextBox32.Text ' For uncomplicated control references The assortment code may not be out of the woods yet. Any controls which are in a container control (like a GroupBox or Panel) will not exist found in Me.Controls; they will be in the Controls drove of that Console or GroupBox. Nor volition a control be returned when the control name is misspelled ("TeStBox2"). In such cases, Cipher will again exist stored in those assortment elements and an NRE will result when you endeavor to reference it.
These should be easy to find now that you know what you are looking for:
"Button2" resides on a Panel
Remedy
Rather than indirect references past name using the form's Controls collection, apply the control reference:
' Declaration Private NameBoxes As TextBox() ' Initialization - simple and easy to read, hard to botch: NameBoxes = New TextBox() {TextBox1, TextBox2, ...) ' Initialize a List NamesList = New Listing(Of TextBox)({TextBox1, TextBox2, TextBox3...}) ' or NamesList = New List(Of TextBox) NamesList.AddRange({TextBox1, TextBox2, TextBox3...}) Function Returning Nothing
Private bars Every bit New Listing(Of Bars) ' Declared and created Public Function BarList() Equally List(Of Confined) confined.Clear If someCondition Then For n As Integer = 0 to someValue bars.Add(GetBar(n)) Next north Else Exit Function End If Return confined End Office This is a case where the IDE will warn you that 'not all paths render a value and a NullReferenceException may outcome'. Y'all tin suppress the warning, by replacing Go out Function with Return Nil, only that does not solve the problem. Anything which tries to utilise the return when someCondition = False will upshot in an NRE:
bList = myFoo.BarList() For Each b Equally Bar in bList ' EXCEPTION ... Remedy
Supplant Exit Function in the function with Return bList. Returning an empty List is not the aforementioned every bit returning Nothing. If in that location is a chance that a returned object tin can be Nothing, test earlier using it:
bList = myFoo.BarList() If bList IsNot Nil Then... Poorly Implemented Endeavor/Take hold of
A badly implemented Try/Grab tin hide where the problem is and result in new ones:
Dim dr As SqlDataReader Try Dim lnk As LinkButton = TryCast(sender, LinkButton) Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow) Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString() ViewState("username") = eid sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle, Pager, mailaddress, from employees1 where username='" & eid & "'" If connection.State <> ConnectionState.Open Then connexion.Open() Stop If command = New SqlCommand(sqlQry, connection) 'More lawmaking fooing and barring dr = command.ExecuteReader() If dr.Read() Then lblFirstName.Text = Convert.ToString(dr("FirstName")) ... Terminate If mpe.Prove() Catch Finally command.Dispose() doctorClose() ' <-- NRE connection.Close() Stop Try This is a case of an object not being created every bit expected, but also demonstrates the counter usefulness of an empty Grab.
At that place is an extra comma in the SQL (afterward 'mailaddress') which results in an exception at .ExecuteReader. After the Grab does zilch, Finally tries to perform make clean up, but since you cannot Close a null DataReader object, a brand new NullReferenceException results.
An empty Catch cake is the devil's playground. This OP was baffled why he was getting an NRE in the Finally cake. In other situations, an empty Take hold of may result in something else much further downstream going haywire and cause yous to spend time looking at the wrong things in the wrong place for the problem. (The "silent exception" described above provides the same entertainment value.)
Remedy
Don't utilise empty Effort/Catch blocks - let the code crash so yous can a) identify the cause b) place the location and c) apply a proper remedy. Try/Catch blocks are non intended to hibernate exceptions from the person uniquely qualified to fix them - the developer.
DBNull is non the same equally Nothing
For Each row Equally DataGridViewRow In dgvPlanning.Rows If Not IsDBNull(row.Cells(0).Value) So ... The IsDBNull office is used to exam if a value equals Arrangement.DBNull: From MSDN:
The System.DBNull value indicates that the Object represents missing or non-existent data. DBNull is not the aforementioned as Nil, which indicates that a variable has not yet been initialized.
Remedy
If row.Cells(0) IsNot Nothing And so ... As before, y'all can test for Nothing, then for a specific value:
If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = Fake) Then Example two
Dim getFoo = (From f In dbContext.FooBars Where f.something = something Select f).FirstOrDefault If Not IsDBNull(getFoo) And so If IsDBNull(getFoo.user_id) Then txtFirst.Text = getFoo.first_name Else ... FirstOrDefault returns the outset detail or the default value, which is Nothing for reference types and never DBNull:
If getFoo IsNot Nada Then... Controls
Dim chk As CheckBox chk = CType(Me.Controls(chkName), CheckBox) If chk.Checked Then Return chk Stop If If a CheckBox with chkName can't be constitute (or exists in a GroupBox), then chk will exist Cypher and be attempting to reference any belongings will consequence in an exception.
Remedy
If (chk IsNot Nix) AndAlso (chk.Checked) Then ... The DataGridView
The DGV has a few quirks seen periodically:
dgvBooks.DataSource = loan.Books dgvBooks.Columns("ISBN").Visible = Truthful ' NullReferenceException dgvBooks.Columns("Title").DefaultCellStyle.Format = "C" dgvBooks.Columns("Author").DefaultCellStyle.Format = "C" dgvBooks.Columns("Price").DefaultCellStyle.Format = "C" If dgvBooks has AutoGenerateColumns = True, it will create the columns, but information technology does non proper name them, so the above code fails when it references them by proper noun.
Remedy
Name the columns manually, or reference by index:
dgvBooks.Columns(0).Visible = True Instance 2 — Beware of the NewRow
xlWorkSheet = xlWorkBook.Sheets("sheet1") For i = 0 To myDGV.RowCount - 1 For j = 0 To myDGV.ColumnCount - i For yard Every bit Integer = 1 To myDGV.Columns.Count xlWorkSheet.Cells(one, k) = myDGV.Columns(k - ane).HeaderText xlWorkSheet.Cells(i + 2, j + one) = myDGV(j, i).Value.ToString() Next Next Next When your DataGridView has AllowUserToAddRows as True (the default), the Cells in the blank/new row at the bottom will all incorporate Nothing. Most attempts to use the contents (for example, ToString) will effect in an NRE.
Remedy
Employ a For/Each loop and test the IsNewRow property to determine if it is that concluding row. This works whether AllowUserToAddRows is true or not:
For Each r Every bit DataGridViewRow in myDGV.Rows If r.IsNewRow = False And so ' ok to use this row If you practise utilize a For n loop, change the row count or use Go out For when IsNewRow is true.
My.Settings (StringCollection)
Under certain circumstances, trying to apply an item from My.Settings which is a StringCollection tin can result in a NullReference the first fourth dimension you use it. The solution is the aforementioned, merely not as obvious. Consider:
My.Settings.FooBars.Add("ziggy") ' foobars is a string collection Since VB is managing Settings for you, it is reasonable to await it to initialize the collection. It volition, only only if yous have previously added an initial entry to the collection (in the Settings editor). Since the drove is (apparently) initialized when an particular is added, it remains Zippo when there are no items in the Settings editor to add.
Remedy
Initialize the settings collection in the form'southward Load event handler, if/when needed:
If My.Settings.FooBars Is Naught Then My.Settings.FooBars = New System.Collections.Specialized.StringCollection Terminate If Typically, the Settings drove will only need to exist initialized the first time the application runs. An alternate remedy is to add together an initial value to your collection in Project -> Settings | FooBars, save the project, and so remove the simulated value.
Key Points
Yous probably forgot the New operator.
or
Something you assumed would perform flawlessly to render an initialized object to your code, did not.
Don't ignore compiler warnings (ever) and apply Option Strict On (always).
MSDN NullReference Exception
Source: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it
Post a Comment for "In Reference to Search Utilities What Does an Index Do"