473,385 Members | 1,736 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,385 software developers and data experts.

error: System.NullReference Exception: Object reference not set

115 100+
hi

i had developed pop3 Account mail view in my web page

but i got error in (System.NullReference Exception: Object reference not set)
NetStrm.Write(szData, 0, szData.Length); - this line
Expand|Select|Wrap|Line Numbers
  1. ///my code start here (cs file)
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.Net;
  15. using System.Net.Sockets;
  16. using System.IO;
  17.  
  18. public partial class _Default : System.Web.UI.Page 
  19. {
  20.     public TcpClient testServername;
  21.     public NetworkStream NetStrm;
  22.     public StreamReader RdStrm;
  23.     public string Data;
  24.     public byte[] szData;
  25.     public string CRLF = "\r\n";    
  26.     protected void Page_Load(object sender, EventArgs e)
  27.     {
  28.  
  29.     }
  30.     protected void Button1_Click(object sender, EventArgs e)
  31.     {
  32.         string strpopservername = txtpopservername.Text;
  33.         string strusername = txtusername.Text;
  34.         string strpassword = txtpassword.Text;
  35.  
  36.         //Cursor cr = Cursor.Current;
  37.         //Cursor.Current = Cursors.WaitCursor;
  38.  
  39.         testServername = new TcpClient(strpopservername, 110);
  40.         //Status.Items.Clear();
  41.  
  42.         try
  43.         {
  44.             // initialization
  45.             NetStrm = testServername.GetStream();
  46.             RdStrm = new StreamReader(testServername.GetStream());
  47.             //Status.Items.Add(RdStrm.ReadLine());
  48.             //Label1.Text = Label1.Text + RdStrm.ReadLine();
  49.             ListBox1.Items.Add(RdStrm.ReadLine());
  50.  
  51.             // Login Process
  52.             Data = "USER " + strusername + CRLF;
  53.             szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  54.             NetStrm.Write(szData, 0, szData.Length);
  55.             //Status.Items.Add(RdStrm.ReadLine());
  56.             //Label1.Text = Label1.Text + RdStrm.ReadLine();
  57.             ListBox1.Items.Add(RdStrm.ReadLine());
  58.  
  59.             Data = "PASS " + strpassword + CRLF;
  60.             szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  61.             NetStrm.Write(szData, 0, szData.Length);
  62.             //Status.Items.Add(RdStrm.ReadLine());
  63.             //Label1.Text = Label1.Text + RdStrm.ReadLine();
  64.             ListBox1.Items.Add(RdStrm.ReadLine());
  65.  
  66.  
  67.             // Send STAT command to get information ie: number of mail and size
  68.             Data = "STAT" + CRLF;
  69.             szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  70.             NetStrm.Write(szData, 0, szData.Length);
  71.             //Status.Items.Add(RdStrm.ReadLine());
  72.             //Label1.Text = Label1.Text + RdStrm.ReadLine();
  73.             ListBox1.Items.Add(RdStrm.ReadLine());
  74.  
  75.  
  76.             // change enabled - disabled button
  77.             Butconnect.Enabled = false;
  78.             Butdisconnect.Enabled = true;
  79.             ButRetrieve.Enabled = true;
  80.             //ConnectBtn.Enabled = false;
  81.            // DisconnectBtn.Enabled = true;
  82.            // RetrieveBtn.Enabled = true;
  83.  
  84.             // back to normal cursor
  85.             //Cursor.Current = cr;
  86.  
  87.         }
  88.         catch (InvalidOperationException err)
  89.         {
  90.             //Status.Items.Add("Error: " + err.ToString());
  91.             //Label1.Text = "Error: " + err.ToString();
  92.             ListBox1.Items.Add("Error: " + err.ToString());
  93.         }
  94.  
  95.     }
  96.     protected void Butdisconnect_Click(object sender, EventArgs e)
  97.     {
  98.  
  99.  
  100.     }
  101.     protected void ButRetrieve_Click(object sender, EventArgs e)
  102.     {
  103.         // change cursor into wait cursor
  104.         //Cursor cr = Cursor.Current;
  105.         //Cursor.Current = Cursors.WaitCursor;
  106.         string szTemp;
  107.         //Message.Clear();
  108.         Label2.Text = "";
  109.         try
  110.         {
  111.             // retrieve mail with number mail parameter
  112.             Data = "RETR " + txtnumber.Text + CRLF;
  113.             szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  114.             //NetStrm.Write(szData, 0, szData.Length);
  115.             NetStrm.Write(szData, 0, szData.Length);
  116.  
  117.             szTemp = RdStrm.ReadLine();
  118.             if (szTemp[0] != '-')
  119.             {
  120.  
  121.                 while (szTemp != ".")
  122.                 {
  123.                     Label2.Text += szTemp + CRLF;
  124.                     szTemp = RdStrm.ReadLine();
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 ListBox1.Items.Add(szTemp);
  130.             }
  131.  
  132.             // back to normal cursor
  133.             //Cursor.Current = cr;
  134.  
  135.         }
  136.         catch (InvalidOperationException err)
  137.         {
  138.             ListBox1.Items.Add("Error: " + err.ToString());
  139.         }
  140.         catch (System.Exception ex)
  141.         {
  142.             ListBox1.Items.Add("Error : " + ex.ToString());
  143.         }
  144.  
  145.     }
  146. }
  147.  
///my code end here

pls guide me

Thanks
Mar 14 '08 #1
4 2058
Plater
7,872 Expert 4TB
Wouldn't you need to Connect() your TcpClient before trying to write data to it?
Mar 14 '08 #2
yogarajan
115 100+
hi

thanks for ur reply

can u send code

pls

thanks

Wouldn't you need to Connect() your TcpClient before trying to write data to it?
Mar 15 '08 #3
Plater
7,872 Expert 4TB
hi

thanks for ur reply

can u send code

pls

thanks
...

testServername.Connect()
Mar 17 '08 #4
r035198x
13,262 8TB
hi

thanks for ur reply

can u send code

pls

thanks
NullReferenceException simply means that you tried to dereference a null. In that line you are dereferencing two objects. NetStr and szData. If you do a Console.WriteLine of those variables before the call you will see which one is null.
Mar 17 '08 #5

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

Similar topics

4
by: roger | last post by:
Here's a weird one... The code below works just fine when I build in DEBUG mode. Today, I tried to build my solution in RELEASE mode, and immediately fell over this problem - apparently...
1
by: Andras Serfozo Jr via .NET 247 | last post by:
(Type your message here) Hello, I am having problems with a Microsoft XML example from VBat the Movies/Data and XML/Read XML Files. I created the program just like in the movie and it works up...
2
by: Seth Broomer | last post by:
Can someone please tell me what might be causeing this error. I get it every once in a while. I just noticed this also...where it says c:\sethbacup\d... that is where i do my build. But then i...
1
by: kscdavefl | last post by:
When I try to display the fields associated with a table using the following code: private void fieldsButton_Click(object sender, System.EventArgs e) { string tableName =...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
1
by: eoinmoon | last post by:
Hi all, New to C# and VS 5 and .NET. I have attempted to write a service via various examples and after several failed attempts to write an installer via the VS 5 wizards,etc I tried to install...
6
by: Steve | last post by:
Hi All I have a windows forms Application (SAM) in vb.net 2008 using .net framework V2 One and only one customer out of 30 customers is getting errors daily where they have to close and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.