Kenny
Kenny

Reputation: 1054

How do I resolve "Object reference not set to an instance of an object" error?

I am passing the following via the request object in codeBehind:

Get the Requested code to be created.

Dim Code As String = Request("code").ToString()

and below is my markup page called barcodes.aspx:

  <form id="Form1" method="post" runat="server">
  <img src="barcodes.aspx?code=XXXXX" alt="barcode" /> 

   <asp:Image id="myBarCode"  runat="server"></asp:Image>
  </form>

Why am I getting the error above?

Upvotes: 0

Views: 3431

Answers (7)

Kettch19
Kettch19

Reputation: 404

You need to actually submit the form (POST) or pass the parameters in the URL (GET) to get the request parameters via the Request object. Two easy ways (of many) to do this:

Option 1. Keep just your barcodes.aspx and barcodes.aspx.vb. In barcodes.aspx put:

<form id="Form1" method="post" runat="server">
  <input type="hidden" name="code" value="XXXXX" /> 

  <asp:Image id="myBarCode"  runat="server"></asp:Image>
  <asp:Button runat="server" Text="Submit"></asp:Button>
</form>

In barcodes.aspx.vb put in the Page_Load:

If PostBack Then
  Dim code As String = Request.Form("code")
  If Not String.IsNullOrEmpty(code) Then
    ' Generate your image here, a code has been specified
  End If
End If

Then just hit the submit button on your aspx page.

Option 2. Split it into two aspx pages but basically same as above.

In submitme.aspx put this (for a POST, click the button):

<form id="Form1" method="post" runat="server">
  <input type="hidden" name="code" value="XXXXX" /> 

  <asp:Button runat="server" Text="Submit" PostBackURL="barcodes.aspx"></asp:Button>
</form>

Or this (for a GET, click the link):

<a href="barcodes.aspx?code=XXXXX">Click Me</a>

In barcodes.aspx.vb put this in Page_Load (works for either the GET or POST option):

Dim code As String = Request("code")
If Not String.IsNullOrEmpty(code) Then
  ' Generate your image here, a code has been specified
End If

In barcodes.aspx you would then simply need:

<asp:Image id="myBarCode"  runat="server"></asp:Image>

Upvotes: 0

jQueryMike
jQueryMike

Reputation: 15

You could also use the string.isnullorempty() function to test the contents of the request.querystring value

Upvotes: 0

Hobbes
Hobbes

Reputation: 147

Check to see that the query string value exists:

Dim Code as String = String.Empty
If Not Request.QueryString("code") Is Nothing Then
    Code = Request.QueryString("code")
End If

Upvotes: 0

peroija
peroija

Reputation: 1982

I think this has to deal with the fact that you are trying to get the query string value of an image on the page. Request() will only get the querystring for the 'requested page', which is the page you are on, not the image itself. Therefore your code will always be null if you are expecting it to pull from the image source

Upvotes: 1

Dima
Dima

Reputation: 1761

You can simply remove ToString(), as Request(item key) returns String:

Dim Code As String = Request("code")

Upvotes: 0

Umair
Umair

Reputation: 3253

Check if Request("code") is null or not. If it is null, then you will get an object reference error when calling ToString()

Upvotes: 1

Daniel Mann
Daniel Mann

Reputation: 59074

If Request("code") is null, calling .ToString() on it will give you a null reference exception.

Upvotes: 3

Related Questions