Object Reference Set To Null

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1628 Views - Last Post: 02 August 2011 - 10:54 AM Rate Topic: -----

#1 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Object Reference Set To Null

Posted 01 August 2011 - 08:05 PM

I keep getting an "Object Reference Set To Null" error when I try to load a picture into a picturebox located on another form.

Heres the code Im working with. Its all located in the DocumentCompletedEvent:

foreach (HtmlElement bob in webBrowser1.document.Images)
{
    if (bob.GetAttribute("src").Contains(""))
{
Form2 f2 = new Form2();
f2.pictureBox1.Load(bob.GetAttribute("src"));
f2.Show();
}
}



Any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference Set To Null

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12315
  • View blog
  • Posts: 45,414
  • Joined: 27-December 08

Re: Object Reference Set To Null

Posted 01 August 2011 - 08:11 PM

Moved to C#. Please reserve the C# Programmers forum for topical, non-help related discussion.
Was This Post Helpful? 0
  • +
  • -

#3 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Object Reference Set To Null

Posted 01 August 2011 - 08:14 PM

Oh sorry thanks for switching it. Does anyone know the answer to my problem?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Object Reference Set To Null

Posted 01 August 2011 - 09:09 PM

Which line is giving you the error?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Object Reference Set To Null

Posted 01 August 2011 - 09:12 PM

I'm guessing it's this line here:

if (bob.GetAttribute("src").Contains(""))


bob.GetAttribute("src") is going to try to find an attribute. If none exists, null will be returned. Calling .Contains on null will throw a NullReferenceException. You're going to need to set the value to a variable, then test for null. If it's not, then see if it contains a value.
Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Object Reference Set To Null

Posted 02 August 2011 - 05:32 AM

Why are you testing to see if the attribute contains nothing? Seems pointless to me.
Was This Post Helpful? 0
  • +
  • -

#7 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Object Reference Set To Null

Posted 02 August 2011 - 07:55 AM

No its not that line I removed the URL of the image on purpose. The line is

f2.pictureBox1.Load(bob.GetAttribute("src"));

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:07 AM

You're making this incredibly hard on us. This is the kind of information you need to provide in your first post.

Now, we need the code for Form2. My new guess is that you're not initializing your PictureBox. Also, it's bad practice to publicly expose GUI controls. Use properties.

Please read the following two articles about how to help us help you:
"It Doesn't Work!" That's not good enough!
It Still Doesn't Work!.
Was This Post Helpful? 1
  • +
  • -

#9 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:13 AM

Form2 is blank it has no code just a picturebox,textbox and a submit button but no code is attached to any object
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:25 AM

Well, of course pictureBox1 is going to be null then. You need to have a constructor in Form2 that initializes everything. If you already have code like this:

public Form2()
{
    InitializeComponent();
}


You need to add a line initializing your picturebox after InitializeComponent. If you don't already have code like that, then you're manually creating your forms (or you're deleting stuff), and I'd need to see what you actually have to give advice.

However, I strongly recommend you research C# Properties, and learn the proper way to deal with exposing fields on Forms (and other classes). Allowing anything but the form itself to modify it's GUI controls is damn poor design. You'll have no idea where a change comes from eventually. If the form itself is the only one responsible for changing itself, then you can debug problems much more efficiently, as well as change display logic in one place instead of all over your code.
Was This Post Helpful? 1
  • +
  • -

#11 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:43 AM

Here is what I have in my form2:

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



namespace Project
{
    public partial class Form2 : Form
    {
   
}
    }



Do you know what I need to add?

P.S: I will research C# properties in a bit, thanks for the recommendation.

This post has been edited by kytro360: 02 August 2011 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:49 AM

Well, did you write this by hand, or did you add a new form through Visual Studio? If you added through VS, then you should have an InitializeComponent call, like I showed. That should actually solve your problem, since that's the method that initializes the form's GUI controls added through the interface builder.

If you wrote this by hand (or copied and pasted it), then I'm really not going to be able to help you much.
Was This Post Helpful? 0
  • +
  • -

#13 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:51 AM

Yes, Curtis told you in the post above, you need to add the InitializeComponent(); method, which basically creates and initializes all the GUI controls, and sorts your form out. If you don't have this, you won't have a picture box, and therefore the object will be null.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace Project
{
    public partial class Form2 : Form
    {
        InitializeComponent();
    }
}



EDIT: Sorry Curtis, you beat me to reply!

This post has been edited by ragingben: 02 August 2011 - 08:52 AM

Was This Post Helpful? 0
  • +
  • -

#14 kytro360   User is offline

  • D.I.C Head

Reputation: -15
  • View blog
  • Posts: 58
  • Joined: 28-July 11

Re: Object Reference Set To Null

Posted 02 August 2011 - 08:54 AM

Im getting a : Method must have return type error
Was This Post Helpful? -4
  • +
  • -

#15 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Object Reference Set To Null

Posted 02 August 2011 - 09:05 AM

I'm seriously getting fed up here. You're not helping us help you.

POST YOUR UPDATED CODE WHEN YOU UPDATE IT!

That way we can see what you might have done wrong. DON'T JUST TELL US AN EXCEPTION NAME. Show us the code, and show us what line it's on!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2