 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tom de Neef Guest
|
Posted: Mon Jan 24, 2005 12:14 am Post subject: Re: Object reference not set to an instance of an object |
|
|
"Danny" <dc (AT) mbox303 (DOT) swipnet.se> schreef in bericht
news:jiWId.16326$Of5.11294 (AT) nntpserver (DOT) swip.net...
| Quote: |
Hi!
I have a really annoying problem. It's from a program I've done in Delphi
5 but now tries to get it working in Delphi 8. I have reproduced the
problem in the following extremly simple program, it has something to do
with the StretchDraw routine. When i run the program and execute the event
(formclick) an error occours:
"Object reference not set to an instance of an object"
Anyone have any ideas what to do. I have spent many hours on the internet
trying to solve that problem, this is my last chance except installing
Delphi 5 again.
Thanks
Daniel
------- The source code for the program that reproduces the error ------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.nfm}
procedure TForm1.FormClick(Sender: TObject);
var n:integer;
bild:TPicture;
begin
bild.LoadFromFile('c:testbild.jpg');
Form1.Canvas.StretchDraw(TRect.Create(1,1,100,150), bild.Graphic);
bild.Free;
end;
end.
|
Bild has not been created when you try loading from file. Insert a
bild.create before the loadFromFile.
The debugger will highlight the line following the one in which the error
occured.
Tom
|
|
| Back to top |
|
 |
AlanGLLoyd Guest
|
Posted: Mon Jan 24, 2005 7:22 am Post subject: Re: Object reference not set to an instance of an object |
|
|
In article <jiWId.16326$Of5.11294 (AT) nntpserver (DOT) swip.net>, "Danny"
<dc (AT) mbox303 (DOT) swipnet.se> writes:
| Quote: | I have reproduced the problem
in the following extremly simple program, it has something to do with the
StretchDraw routine.
|
That's where it surfaces, but your problem is that Bild is a variable holding a
reference (ie a place to store an actual occurence) of an object. For such a
non-automatially ceated object, _you_ must create the object. When you call
Create on an object it returns a reference to that object which you must place
in the variable.
Place ...
Bild := TPicture.Create;
.... before your call to load the file.
As _you_ have created the object, then _you_ must free it - and this you have
done.
Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Mon Jan 24, 2005 10:53 am Post subject: Re: Object reference not set to an instance of an object |
|
|
....ok, interesting reading! I thought I've done something wrong with
StretchDraw. Well, now it works when I created it. Thanks!
"AlanGLLoyd" <alanglloyd (AT) aol (DOT) com> wrote
| Quote: | In article <jiWId.16326$Of5.11294 (AT) nntpserver (DOT) swip.net>, "Danny"
[email]dc (AT) mbox303 (DOT) swipnet.se[/email]> writes:
I have reproduced the problem
in the following extremly simple program, it has something to do with the
StretchDraw routine.
That's where it surfaces, but your problem is that Bild is a variable
holding a
reference (ie a place to store an actual occurence) of an object. For such
a
non-automatially ceated object, _you_ must create the object. When you
call
Create on an object it returns a reference to that object which you must
place
in the variable.
Place ...
Bild := TPicture.Create;
... before your call to load the file.
As _you_ have created the object, then _you_ must free it - and this you
have
done.
Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]
|
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Mon Jan 24, 2005 2:07 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
I'm sorry to bother you again. But now I have implemented the code in my
actual project, and it doesn't work. It seems to be another problem now. But
the same error dialog appears. I have tried some different things and the
problem seems to be in the picture loading or something. I have now put the
whole code from my actual project into this testprogram, I can't understand
what's causing the problem, I might try some other way of printing the
pictures.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, System.ComponentModel, Printers;
type
TForm1 = class(TForm)
PrintDialog1: TPrintDialog;
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.nfm}
procedure TForm1.FormClick(Sender: TObject);
var cw, // canvas.width
ch, // canvas.height
rx, // räknare för kolumn (x-axel)
ry, // räknare för rad (y-axel)
b, // bildnummer
p, // sidnummer
x1,x2,y1,y2 // bildens koordinater
:integer;
bild: TBitmap;
namn: String;
begin
PrintDialog1.Execute;
With Printer Do Begin
Title:=('Utskrift');
cw := PageWidth;
ch := PageHeight;
rx:=1;
ry:=1;
p:=1;
BeginDoc;
Canvas.Brush.Style:=bsClear;
Canvas.Font.Height:=Round(ch/50);
Canvas.Font.Color:=clBlack;
Canvas.TextOut(Round(cw/20),Round(ch/40),'Testskolan, testklass');
Canvas.Font.Height:=Round(ch/90);
For b:=0 To 10 Do Begin
bild := TBitmap.Create;
bild.LoadFromFile('c:testport.jpg');
namn := 'testnamn';
x1 := Round( cw/20 + (cw/5.3*(rx-1)) );
x2 := Round( x1 + (cw/6.7) );
y1 := Round( ch/15 + (ch/5.5*(ry-1)) );
y2 := Round( y1 + ((x2-x1)*1.25) );
Canvas.StretchDraw(TRect.Create(x1,y1,x2,y2),bild);
Canvas.Rectangle(TRect.Create(x1,y1,x2,y2));
bild.Free;
If Length(namn)<24 Then
Canvas.TextOut(x1, Round(y1+(ch/7.3)), namn)
Else Begin
If Length(namn)>48 Then Delete(namn,49,length(namn));
Canvas.TextOut(x1, Round(y1+(ch/7.10)), Copy(namn,0,24));
Canvas.TextOut(x1, Round(y1+(ch/6.45)),
Copy(namn,25,Length(namn)));
End;
If rx=5 Then Begin
Inc(ry);
rx:=1;
End Else Inc(rx);
If ry=6 Then Begin
Canvas.Font.Height:=Round(ch/80);
Canvas.Textout(Round(cw-(cw/11.4)), Round(ch-(ch/27)), 'Sida
'+IntToStr(p));
Printer.NewPage;
Inc(p);
Canvas.Textout(Round(cw-(cw/11.4)), Round(ch-(ch/27)), 'Sida
'+IntToStr(p));
ry:=1;
Canvas.Font.Height:=Round(ch/90);
End;
End;
EndDoc;
End;
end;
end.
|
|
| Back to top |
|
 |
Tom de Neef Guest
|
Posted: Mon Jan 24, 2005 2:39 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
Comments in the code ...
Tom
"Danny" <dc (AT) mbox303 (DOT) swipnet.se> schreef in bericht
news:Zi7Jd.16475$Of5.11284 (AT) nntpserver (DOT) swip.net...
| Quote: |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, System.ComponentModel, Printers;
type
TForm1 = class(TForm)
PrintDialog1: TPrintDialog;
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.nfm}
procedure TForm1.FormClick(Sender: TObject);
var cw, // canvas.width
ch, // canvas.height
rx, // räknare för kolumn (x-axel)
ry, // räknare för rad (y-axel)
b, // bildnummer
p, // sidnummer
x1,x2,y1,y2 // bildens koordinater
:integer;
bild: TBitmap;
namn: String;
begin
PrintDialog1.Execute;
With Printer Do Begin
Title:=('Utskrift');
cw := PageWidth;
ch := PageHeight;
rx:=1;
ry:=1;
p:=1;
BeginDoc;
Canvas.Brush.Style:=bsClear;
Canvas.Font.Height:=Round(ch/50);
Canvas.Font.Color:=clBlack;
Canvas.TextOut(Round(cw/20),Round(ch/40),'Testskolan, testklass');
Canvas.Font.Height:=Round(ch/90);
For b:=0 To 10 Do
Begin
bild := TBitmap.Create; // You could move the create outside the
loop
bild.LoadFromFile('c:testport.jpg'); // Can a Tbitmap load a
JPG ?
namn := 'testnamn';
x1 := Round( cw/20 + (cw/5.3*(rx-1)) );
x2 := Round( x1 + (cw/6.7) );
y1 := Round( ch/15 + (ch/5.5*(ry-1)) );
y2 := Round( y1 + ((x2-x1)*1.25) );
// Canvas.StretchDraw(TRect.Create(x1,y1,x2,y2),bild); |
// Canvas.Rectangle(TRect.Create(x1,y1,x2,y2));
Canvas.StretchDraw(Rect(x1,y1,x2,y2),bild); // check out Rect
function
Canvas.Rectangle(Rect(x1,y1,x2,y2));
| Quote: | bild.Free; // and move the free outside the loop as well
If Length(namn)<24 Then
Canvas.TextOut(x1, Round(y1+(ch/7.3)), namn)
Else Begin
// If Length(namn)>48 Then Delete(namn,49,length(namn)); |
If Length(namn)>48 Then SetLength(namn,49);
// Canvas.TextOut(x1, Round(y1+(ch/7.10)), Copy(namn,0,24));
Canvas.TextOut(x1, Round(y1+(ch/7.10)), Copy(namn,1,24)); //
1-based string
| Quote: | Canvas.TextOut(x1, Round(y1+(ch/6.45)),
// Copy(namn,25,Length(namn))); |
Copy(namn,26,maxInt));
| Quote: | End;
If rx=5 Then Begin
Inc(ry);
rx:=1;
End Else Inc(rx);
If ry=6 Then Begin
Canvas.Font.Height:=Round(ch/80);
Canvas.Textout(Round(cw-(cw/11.4)), Round(ch-(ch/27)), 'Sida
'+IntToStr(p));
Printer.NewPage; // You're still in 'with printer do', so
NewPage suffices
Inc(p);
Canvas.Textout(Round(cw-(cw/11.4)), Round(ch-(ch/27)), 'Sida
'+IntToStr(p));
ry:=1;
Canvas.Font.Height:=Round(ch/90);
End;
End // of for b loop
EndDoc;
End;
end;
end.
|
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Mon Jan 24, 2005 4:25 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
Hi and thanks for the look... but it's still the same error (as in the
subject, "object reference").
About your comments:
- Yes in Delphi 8 you can a jpg in a TBitmap.
- The StretchDraw(TRect.Create... must bu used, because it's different from
early Delphi where you could use Rect only.
- From the beginning I had bild.create/free outside the loop, thought that
it was better myself, that was a new testcode.
- The other changes where interesting, I'll try those.
Thanks
"Tom de Neef" <tdeneef (AT) qolor (DOT) nl> wrote
| Quote: | Comments in the code ...
Tom
|
|
|
| Back to top |
|
 |
ronenbitman@gmail.com Guest
|
Posted: Tue Jan 25, 2005 9:34 am Post subject: Re: Object reference not set to an instance of an object |
|
|
Danny Firstly in Delphi when you create an object you should do it this
way (this way your sure it will be freed even if there is an error on
the way)
bild := TBitmap.Create
try
For b := ....
Begin
....
....
Bild.LoadFromFile....
....
....
End;
Finally
Bild.Free;
End;
that should probably fix it?
Hope i helped
|
|
| Back to top |
|
 |
VBDis Guest
|
Posted: Tue Jan 25, 2005 10:19 am Post subject: Re: Object reference not set to an instance of an object |
|
|
Im Artikel <jiWId.16326$Of5.11294 (AT) nntpserver (DOT) swip.net>, "Danny"
<dc (AT) mbox303 (DOT) swipnet.se> schreibt:
| Quote: | "Object reference not set to an instance of an object"
|
Did you use "with" in your code? Then remove it and try again...
DoDi
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Tue Jan 25, 2005 11:15 am Post subject: Re: Object reference not set to an instance of an object |
|
|
....mkay... that was a kinda good clue... I'll try...
| Quote: | Danny Firstly in Delphi when you create an object you should do it this
way (this way your sure it will be freed even if there is an error on
the way)
bild := TBitmap.Create
try
For b := ....
Begin
...
...
Bild.LoadFromFile....
...
...
End;
Finally
Bild.Free;
End;
that should probably fix it?
Hope i helped
|
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Tue Jan 25, 2005 1:41 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
....yes I did... Now I have tried to remove "with" (With Printer Do...) and
put "Printer." before all print-commands instead, but the problem remains...
"VBDis" <vbdis (AT) aol (DOT) com> wrote
| Quote: | Im Artikel <jiWId.16326$Of5.11294 (AT) nntpserver (DOT) swip.net>, "Danny"
[email]dc (AT) mbox303 (DOT) swipnet.se[/email]> schreibt:
"Object reference not set to an instance of an object"
Did you use "with" in your code? Then remove it and try again...
DoDi
|
|
|
| Back to top |
|
 |
Danny Guest
|
Posted: Tue Jan 25, 2005 1:51 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
Now I have tried that too. Actually, now it creates a document when I run
the print routine (the code here). But the document is empty without
pictures and text, but before it didn't even create a document, just a
printer error message (a log file from acrobat in my case). So I guess it's
might be a good idea with try-finally, I'll put it in my code now which I'm
developing in Delphi 7 (where it works. as you might now, the problem
started when I went from D5 to D .
Thanks anyway
/Daniel
<ronenbitman (AT) gmail (DOT) com> wrote
| Quote: | Danny Firstly in Delphi when you create an object you should do it this
way (this way your sure it will be freed even if there is an error on
the way)
bild := TBitmap.Create
try
For b := ....
Begin
...
...
Bild.LoadFromFile....
...
...
End;
Finally
Bild.Free;
End;
that should probably fix it?
Hope i helped
|
|
|
| Back to top |
|
 |
Jamie Guest
|
Posted: Tue Jan 25, 2005 8:25 pm Post subject: Re: Object reference not set to an instance of an object |
|
|
Danny wrote:
| Quote: | Now I have tried that too. Actually, now it creates a document when I run
the print routine (the code here). But the document is empty without
pictures and text, but before it didn't even create a document, just a
printer error message (a log file from acrobat in my case). So I guess it's
might be a good idea with try-finally, I'll put it in my code now which I'm
developing in Delphi 7 (where it works. as you might now, the problem
started when I went from D5 to D .
Thanks anyway
/Daniel
[email]ronenbitman (AT) gmail (DOT) com[/email]> wrote in message
news:1106645689.135499.238540 (AT) c13g2000cwb (DOT) googlegroups.com...
Danny, in some cases there are printers that will not work very well |
when usng a DDB bitmap. they need a DIB at 24 bits.
just before you get ready to send the image to the printer code try this.
theBitmap.pixelform := pf24bit;
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|