473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Object reference not set to an instance of an object

3
I am getting an error "object reference not set to an instance of an object"

Expand|Select|Wrap|Line Numbers
  1. Partial Class dynamic_array
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Dim s() As String
  5.     Dim i As Integer = 0
  6.  
  7.     Protected Sub btn_set_Click(sender As Object, e As System.EventArgs) Handles btn_set.Click
  8.  
  9.         ReDim s(CInt(txt_no.Text))
  10.     End Sub
  11.  
  12.     Protected Sub btn_save_Click(sender As Object, e As System.EventArgs) Handles btn_save.Click
  13.         Dim i As Integer = 0
  14.  
  15.         Try
  16.             For i = 0 To s.Length - 1
  17.                 s(i) = InputBox("enter name " + (i + 1).ToString)
  18.             Next
  19.         Catch ex As Exception
  20.             MsgBox(ex.Message)
  21.         End Try
  22.  
  23.  
  24.     End Sub
  25.  
  26.     Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
  27.  
  28.         Dim str As String = ""
  29.  
  30.         Dim i As Integer = 0
  31.  
  32.         Try
  33.             For i = 0 To s.Length - 1
  34.                 str &= s(i) + vbCrLf
  35.             Next
  36.  
  37.             MsgBox(str)
  38.  
  39.         Catch ex As Exception
  40.             MsgBox(ex.Message)
  41.  
  42.         End Try
  43.     End Sub
  44. End Class
Dec 6 '13 #1
1 1448
Frinavale
9,735 Expert Mod 8TB
Hi there!

You are experiencing a NullRefereceException. This exception is thrown when you attempt to use a variable that you have declared but have not instantiated it beforehand.

For example, the following will throw a NullRefrenceException:
Expand|Select|Wrap|Line Numbers
  1. Dim x As SomeObject
  2. x.PropertyOfSomeObject = "a value"
The reason the exception would be thrown in this case is because I am attempting to set the PropertyOfSomeObject property to something but have not instantiated x yet.

The following code will NOT throw the exception because I have instantiated x before attempting to use it:
Expand|Select|Wrap|Line Numbers
  1. Dim x As SomeObject
  2. x = new SomeObject()
  3. x.PropertyOfSomeObject = "a value"
I could have simplified that to:
Expand|Select|Wrap|Line Numbers
  1. Dim x As New SomeObject
  2. x.PropertyOfSomeObject = "a value"

In your praticular case you have declared an array of string's s on line 4 and have not instantiated it before you attempt to access it on line 17.

Consider instantiating this array in the Page Load Event.

-Frinny

(PS. using MsgBox in a web application will not display a message to the user accessing the web application through a web browser)
Dec 10 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
9
by: Remulac | last post by:
Hello, I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object reference not set...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
2
by: ankur seth via DotNetMonster.com | last post by:
Getting error Null Reference exception when a procedure is called in vb.net ..Why does it occur and how to resolve it. --- ANkur -- Message posted via http://www.dotnetmonster.com
1
by: somersbar | last post by:
i have a webform with a dropdownlist and 2 textboxes. it has a dataset with a table with 2 fields, 'areaID' and 'areaName'. in the Data properties window of the DDL, i have the DataTextField set...
9
by: bill | last post by:
I keep getting Object reference not set to an instance of an object error when trying to run my application on an installed client machine. I installed it on several others and it runs fine. I...
10
by: Arpan | last post by:
Consider the following code that adds a table to a DataSet dynamically: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
2
by: rksadhi | last post by:
/*Geting error ---object reference not set to an instance---at bold line----plz reply asap thanks in advance*/ cmd = new OleDbCommand ("SELECT e.emp_id,e.email, m.email AS Email FROM emp_details...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.