473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error "Object reference not set to an instance of an object"

When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
0, 0)

Why is this?

Marc
Nov 19 '05 #1
18 28726
Dim g as New System.Drawing. Graphics

"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
0, 0)

Why is this?

Marc

Nov 19 '05 #2
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing. Grapichics.Draw String

or import System.Drawing. Graphics and just call DrawString.
"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
0, 0)

Why is this?

Marc

Nov 19 '05 #3
Another error I'm afraid (with both solutions):

"Reference to a non-shared member requires an object reference."

Marc

"CJ Taylor" <ct*****@morton welding.com> schreef in bericht
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing. Grapichics.Draw String

or import System.Drawing. Graphics and just call DrawString.
"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black, 0, 0)

Why is this?

Marc


Nov 19 '05 #4
Ahhh ok...

You have to get the drawing object from somewhere in your application. it
usually comes from a repaint or such. I forgot about little things like
this (its still early).

Like, a repaint where PaintEventArgs is passed.
"Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:ub******** *****@TK2MSFTNG P10.phx.gbl...
Another error I'm afraid (with both solutions):

"Reference to a non-shared member requires an object reference."

Marc

"CJ Taylor" <ct*****@morton welding.com> schreef in bericht
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing. Grapichics.Draw String

or import System.Drawing. Graphics and just call DrawString.
"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:et******** ******@TK2MSFTN GP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black, 0, 0)

Why is this?

Marc



Nov 19 '05 #5
Hello,

"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black, 0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphi cs()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromIm age(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #6
Hello,

"CJ Taylor" <ct*****@morton welding.com> schrieb:
Dim g as New System.Drawing. Graphics


Did you try this?

;-)))

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #7
Hmm... I forgot about that too. . Thanks Herfriend! Always a help from
pulling us out of digging a hole. =)
"Herfried K. Wagner [MVP]" <hi*******@m.ac tivevb.de> wrote in message
news:ue******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphi cs()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromIm age(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Nov 19 '05 #8
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing. Printing.PrintD ocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromIm age(bmp)

Sub Print()
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,
Brushes.Black, 0, 0)
PrintDocument1. Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.ac tivevb.de> schreef in bericht
news:ue******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphi cs()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromIm age(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Nov 19 '05 #9
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.P rintPage
event. This event has a PrintPageEventA rgs parameter. The PrintPageEventA rgs
object has a Graphics property. You need to draw on this Graphics object to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:
http://msdn.microsoft.com/library/de...et12242002.asp
http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
news:uz******** ******@tk2msftn gp13.phx.gbl...
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing. Printing.PrintD ocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromIm age(bmp)

Sub Print()
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,
Brushes.Black, 0, 0)
PrintDocument1. Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.ac tivevb.de> schreef in bericht
news:ue******** ******@TK2MSFTN GP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing. Graphics
g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphi cs()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromIm age(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2817
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little experimentation shows that I cannot run ANY .NET web project without getting this error: ...
0
2075
by: Martin Widmer | last post by:
Hello again! I have a datagridview control on my form and am using VS.Net 2005. One column is set up as combo box column, and when I try to set the datasource for that combobox column at design time in the properties section of VS, I get the error "object reference not set to instance of an object" and the only option is to click OK. I...
35
3189
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an instance of an object" for the line 'If mpg.FindControl("lkred").Visible = True Then'. I couldn't find sofar the solution. Any help would be...
2
6482
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005". so I configured the...
3
2978
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005". so I configured the...
1
1706
by: nguyentrongkha | last post by:
I have two asp.net applications that host on server 2003. I create a hyper link on one application that links to default page of the other application.Whenever users click on that link, I get a error "Object reference not set to an instance of an object" from the application. When I type the link of the application address directly in browser...
11
7452
by: narpet | last post by:
Hello all, I'm hoping somebody can help me with this. I have an app that I've been developing in MS Visual Studio 2005 Pro, C#. I have connectivity to a SQL server database. All of the sudden, whenever I try to add a new or change an existing data binding to a text box (as an example), it gives me the error, "object reference not set to an...
3
2323
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose for a project, but I keep getting the error: "Object reference not set to an instance of an object" for the 7th line of the code below which is:...
5
3819
by: dotnetnovice | last post by:
Hi everybody actually i was trying to insert some records in to SQL server through C# Wndows Aplication and during that i face an error "Object reference not set to an instance of an object." and need help from senors please explain me about it and also indicate it in my code. Here is my code in my Class Named DBHandler!!! using System;...
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.