error: System.NullReference Exception: Object reference not set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yogarajan
    New Member
    • Apr 2007
    • 115

    error: System.NullReference Exception: Object reference not set

    hi

    i had developed pop3 Account mail view in my web page

    but i got error in (System.NullRef erence Exception: Object reference not set)
    NetStrm.Write(s zData, 0, szData.Length); - this line
    Code:
    ///my code start here (cs file)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    
    public partial class _Default : System.Web.UI.Page 
    {
        public TcpClient testServername;
        public NetworkStream NetStrm;
        public StreamReader RdStrm;
        public string Data;
        public byte[] szData;
        public string CRLF = "\r\n";	
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string strpopservername = txtpopservername.Text;
            string strusername = txtusername.Text;
            string strpassword = txtpassword.Text;
            
            //Cursor cr = Cursor.Current;
            //Cursor.Current = Cursors.WaitCursor;
    
            testServername = new TcpClient(strpopservername, 110);
            //Status.Items.Clear();
    
            try
            {
                // initialization
                NetStrm = testServername.GetStream();
                RdStrm = new StreamReader(testServername.GetStream());
                //Status.Items.Add(RdStrm.ReadLine());
                //Label1.Text = Label1.Text + RdStrm.ReadLine();
                ListBox1.Items.Add(RdStrm.ReadLine());
    
                // Login Process
                Data = "USER " + strusername + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                //Status.Items.Add(RdStrm.ReadLine());
                //Label1.Text = Label1.Text + RdStrm.ReadLine();
                ListBox1.Items.Add(RdStrm.ReadLine());
    
                Data = "PASS " + strpassword + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                //Status.Items.Add(RdStrm.ReadLine());
                //Label1.Text = Label1.Text + RdStrm.ReadLine();
                ListBox1.Items.Add(RdStrm.ReadLine());
    
    
                // Send STAT command to get information ie: number of mail and size
                Data = "STAT" + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                //Status.Items.Add(RdStrm.ReadLine());
                //Label1.Text = Label1.Text + RdStrm.ReadLine();
                ListBox1.Items.Add(RdStrm.ReadLine());
    
    
                // change enabled - disabled button
                Butconnect.Enabled = false;
                Butdisconnect.Enabled = true;
                ButRetrieve.Enabled = true;
                //ConnectBtn.Enabled = false;
               // DisconnectBtn.Enabled = true;
               // RetrieveBtn.Enabled = true;
    
                // back to normal cursor
                //Cursor.Current = cr;
    
            }
            catch (InvalidOperationException err)
            {
                //Status.Items.Add("Error: " + err.ToString());
                //Label1.Text = "Error: " + err.ToString();
                ListBox1.Items.Add("Error: " + err.ToString());
            }
    
        }
        protected void Butdisconnect_Click(object sender, EventArgs e)
        {
            
    
        }
        protected void ButRetrieve_Click(object sender, EventArgs e)
        {
            // change cursor into wait cursor
            //Cursor cr = Cursor.Current;
            //Cursor.Current = Cursors.WaitCursor;
            string szTemp;
            //Message.Clear();
            Label2.Text = "";
            try
            {
                // retrieve mail with number mail parameter
                Data = "RETR " + txtnumber.Text + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                //NetStrm.Write(szData, 0, szData.Length);
                NetStrm.Write(szData, 0, szData.Length);
    
                szTemp = RdStrm.ReadLine();
                if (szTemp[0] != '-')
                {
    
                    while (szTemp != ".")
                    {
                        Label2.Text += szTemp + CRLF;
                        szTemp = RdStrm.ReadLine();
                    }
                }
                else
                {
                    ListBox1.Items.Add(szTemp);
                }
    
                // back to normal cursor
                //Cursor.Current = cr;
    
            }
            catch (InvalidOperationException err)
            {
                ListBox1.Items.Add("Error: " + err.ToString());
            }
            catch (System.Exception ex)
            {
                ListBox1.Items.Add("Error : " + ex.ToString());
            }
    
        }
    }
    ///my code end here

    pls guide me

    Thanks
    Last edited by Plater; Mar 14 '08, 01:08 PM. Reason: code tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Wouldn't you need to Connect() your TcpClient before trying to write data to it?

    Comment

    • yogarajan
      New Member
      • Apr 2007
      • 115

      #3
      hi

      thanks for ur reply

      can u send code

      pls

      thanks

      Originally posted by Plater
      Wouldn't you need to Connect() your TcpClient before trying to write data to it?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by yogarajan
        hi

        thanks for ur reply

        can u send code

        pls

        thanks
        ...

        testServername. Connect()

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by yogarajan
          hi

          thanks for ur reply

          can u send code

          pls

          thanks
          NullReferenceEx ception 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.WriteLi ne of those variables before the call you will see which one is null.

          Comment

          Working...