471,120 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 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 1818
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

Post your reply

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

Similar topics

1 post views Thread by Andras Serfozo Jr via .NET 247 | last post: by
2 posts views Thread by Seth Broomer | last post: by
1 post views Thread by kscdavefl | last post: by
4 posts views Thread by XML newbie: Urgent pls help! | last post: by
reply views Thread by XML newbie: Urgent pls help! | last post: by
6 posts views Thread by Steve | last post: by

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.