SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to include an Image control within my web part. In EnsureChildControls I have the following code:

Image image = new Image();
image.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "hotline.jpg");

I get a NullPointer exception and this is because 'Page' is null.

Why might this be? Am I doing this all wrong anyway?

share|improve this question
up vote 1 down vote accepted

Be sure not to call EnsureChildControls() from within the constructor of your WebPart.

Also, I would not override EnsureChildControls but override CreateChildControls instead, as James and Wictor have already mentioned in their answers.

share|improve this answer

Are you calling the parent EnsureChildControls() at the start of your overridden method?

Also, have you tried your code in CreateChildControls()?

share|improve this answer

Can you give us some more details on how you run this Web Part, for instance in which context?

How do you call EnsureChildControls and why are you adding the image there and not in CreateChildControls?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.