-
"Object reference not set to an instance of an object"
I'm trying to set up code to accept credit cards, submit them to a processing gateway, then receive feedback from the gateway.
I received sample code from the gateway company. But when I put the sample page on my ISP's server, I get the following error:
"Object reference not set to an instance of an object"
and it points to the line of code that closes the request streamwriter:
myWriter.Close()
Here's the full code:
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(Src As [Object], E As EventArgs)
myPage.Text = readHtmlPage("https://test.mygateway.net/gateway/transact.dll")
'Uncomment the line ABOVE for shopping cart testing OR uncomment the line BELOW for live accounts
'myPage.Text = readHtmlPage("https://secure.mygateway.net/gateway/transact.dll")
End Sub 'Page_Load
Private Function readHtmlPage(url As String) As [String]
Dim result As [String] = ""
Dim strPost As [String] = "x_login=YOUR-LOGIN-ID&x_tran_key=YOUR-TRANSACTION-KEY&x_method=CC&x_type=AUTH_CAPTURE&x_amount=1.00&x_delim_data=TRUE&x_delim_char=|&x_relay _response=FALSE&x_card_num=4111111111111111&x_exp_date=052009&x_test_request=TRUE&x_versio n=3.1"
Dim myWriter As StreamWriter = Nothing
Dim objRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
objRequest.Method = "POST"
objRequest.ContentLength = strPost.Length
objRequest.ContentType = "application/x-www-form-urlencoded"
Try
myWriter = New StreamWriter(objRequest.GetRequestStream())
myWriter.Write(strPost)
Catch e As Exception
Return e.Message
Finally
myWriter.Close()
End Try
Dim objResponse As HttpWebResponse = CType(objRequest.GetResponse(), HttpWebResponse)
Dim sr As New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()
' Close and clean up the StreamReader
sr.Close()
Return result
End Function 'readHtmlPage
</script>
<html>
<body>
<b>The content on this web page is the result of an HTTP POST operation to Authorize.Net, using the Advanced Implementation method (AIM).<br>
<br/>
</b><hr/>
<asp:literal id="myPage" runat="server"/>
</body>
</html>
Anyone know what the problem here is?
-
The line:
myWriter = New StreamWriter(objRequest.GetRequestStream())
is throwing an exception, so when the Finally block tries to call the .Close method, the myWriter variable is null. Check the value of e.Message to see what the exception is.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
how?
Hi, Phil,
How do I view the value of e? I only get this error when the page is on my ISP, so I don't get the error when I'm developing on my laptop. I tried adding the line:
response.write(e)
after Return e.Message, but the browser just returns the message:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 181: response.Write(e)
Line 182: Finally
Line 183: myWriter.Close()
Line 184: End Try
Line 185:
Source File: E:\kunden\homepages\10\d121267577\transact\vbnet.aspx Line: 183
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.vbnet_aspx.readHtmlPage(String url) in E:\kunden\homepages\10\d121267577\transact\vbnet.aspx:183
ASP.vbnet_aspx.Page_Load(Object Src, EventArgs E) in E:\kunden\homepages\10\d121267577\transact\vbnet.aspx:159
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
-
You need to put Response.Write(e.Message) before Return(e.Message). Once your code hits the Return statement, nothing else executes except the Finally block.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
I switched the return and the response.write statements so that the response.write comes first, but the application still does the same thing, returns "object reference not set to instance of an object" and points to the line where I close the streamreader.
-
Now I've commented a bunch of code out to pinpoint where the app is dying, and it chokes on this line:
myWriter = New StreamWriter(objRequest.GetRequestStream())
What does this error mean:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Source Error:
Line 180: 'Try
Line 181: 'myWriter = New StreamWriter(objRequest.GetRequestStream())
Line 182: myWriter = New StreamWriter(objRequest.GetRequestStream())
Line 183: myWriter.Write(strPost)
Line 184: Response.Write(strPost)
Source File: E:\mypath.aspx Line: 182
Stack Trace:
[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +1002146
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +33
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +431
[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.GetRequestStream() +1504525
ASP.vbnet_aspx.readHtmlPage(String url) in E:\kunden\homepages\10\d121267577\transact\vbnet.aspx:182
ASP.vbnet_aspx.Page_Load(Object Src, EventArgs E) in E:\kunden\homepages\10\d121267577\transact\vbnet.aspx:161
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
-
Same Issue
Was this issue resolved?
When I run the AIM test on my workstation it's fine.
But when I run the AIM test from my development server I receive the same error you do.
Any help would be great.
Thanks
-
What OS is on your development server?
-
Doing some Google searches I found the info below, fixed my issue.
PS Thanks Lois Lane for this post.
Dim myProxy As New Net.WebProxy()
Dim MyUri As New Uri("SERVER:PORT")
myProxy.Address = MyUri
myProxy.Credentials = New Net.NetworkCredential ("USERNAME", "PASSWORD")
objRequest.Proxy = myProxy
Similar Threads
-
Replies: 1
Last Post: 08-16-2006, 06:45 PM
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
By mshahkensington in forum .NET
Replies: 0
Last Post: 01-24-2006, 01:14 PM
-
By Jaco de Villiers in forum XML
Replies: 1
Last Post: 06-01-2001, 05:50 PM
-
By Ed Pinto in forum VB Classic
Replies: 5
Last Post: 03-22-2000, 12:52 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|