2 Replies - 938 Views - Last Post: 23 January 2012 - 10:40 AM Rate Topic: -----

#1 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 381
  • Joined: 27-December 08

Web Client error. System.net problem

Posted 23 January 2012 - 10:03 AM

Hello guys and babes(if they are),

I got a problem with this code. I was downloading a file using following code and when I minimized the visual studio window, I got a null reference expectation error.
Object reference not set to an instance of an object


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Downloader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            textBox2.Text = "c:\\" + GetFileName(textBox1.Text);
                      
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
           
            webClient.DownloadFileAsync(new Uri(textBox1.Text),textBox2.Text);
        }
        private string GetFileName(string hrefLink)
        {
            string[] parts = hrefLink.Split('/');
            string fileName = "";

            if (parts.Length > 0)
                fileName = parts[parts.Length - 1];
            else
                fileName = hrefLink;

            return fileName;
        }

        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Form1.ActiveForm.Text = "Downloader" + " " + e.ProgressPercentage.ToString() + "%";
            label2.Text = e.BytesReceived.ToString() + "/" + e.TotalBytesToReceive.ToString();
            progress.Value = e.ProgressPercentage;
        }
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download completed!");
        }
    }
}


It happens whenever I try to close or open another app like mozilla during the run time of this code.
There's another problem with release version but first I want this shit solved.
So, how do I take care of this error.

Thanks in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Web Client error. System.net problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,365
  • Joined: 02-June 10

Re: Web Client error. System.net problem

Posted 23 January 2012 - 10:10 AM

Covered in the first link below.

Learning to debug one's own code is an essential skill. Sadly, one that apparently no college course teaches. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.
TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Web Client error. System.net problem

Posted 23 January 2012 - 10:40 AM

Is the error occurring on this line?

Form1.ActiveForm.Text = "Downloader" + " " + e.ProgressPercentage.ToString() + "%";



If so, then you should read up on the ActiveForm property, and perhaps see how it could be null.

Also, as tlhIn`toq states, learning how to debug is an excellent skill that is necessary to be even a decent developer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1