[Solved] Assigning NULL to an Object [Calc] Use NOTHING

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

[Solved] Assigning NULL to an Object [Calc] Use NOTHING

Post by Herb40 »

I apparently have a fundamental misunderstanding about my failure to be able to assign the NULL value to an Object. In the following code, the spreadsheet mSS and the sheet mSheet are assigned a SS object and a sheet object, as expected. But my code is not allowed to assign NULL to mSheet in the last line of code. Instead, I get the error message "Object variable not set." I thought NULL was a special value that could be assigned to something, but the error message seems to be saying that NULL is an object variable that has not been set. ???

I need to pass mSheet to another function (in a more complicated situation than shown here). Sometimes there is an actual sheet but at other times there is no sheet, and I need to pass a Null value to indicate this. After the SS is closed, mSheet still refers to the original object, as one would expect, since mSS.close(True) does not "know" what use was being made of its sheets.

What's a workable way to do this? As I said, I seem to have a basic misunderstanding about this. Thanks for your help.

Code: Select all

Sub NullTest
	Dim mSS as Object
	Dim mSheet as Object

	mSS = StarDesktop.LoadComponentFromUrl( "private:factory/scalc", "_blank", 0, Array())
	mSheet = mSS.Sheets(0)	
	mSS.close(True)
	mSheet = Null   ' fails: Object variable not set
End Sub
Last edited by Herb40 on Wed Feb 22, 2017 9:33 pm, edited 2 times in total.
OpenOffice 4.1.3 on Windows 10
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Assigning NULL to an Object [Calc]

Post by RoryOF »

I'm fairly sure NULL is not assignable. Perhaps you should test for the existence of mSS.

This thread may be helpful
viewtopic.php?f=25&t=58429
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Assigning NULL to an Object [Calc]

Post by RPG »

Hello

In BASIC there is an object NOTHING. Maybe it can work.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

Re: Assigning NULL to an Object [Calc]

Post by Herb40 »

Thanks, fellows!

Here's an example of a typical situation I have:

Code: Select all

if mSS.Sheets.hasByName("sheet name") then
  mSheet = mSS.Sheets.getByName("sheet name")
else
  mSheet = NOTHING
end if
MyFunction(mSS, mSheet, ...)
I need to discover in MyFunction whether the second argument is Null.

Fortunately, using NOTHING works just fine in the assignment to mSheet. 'Never heard of it before.
OpenOffice 4.1.3 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Post by JeJe »

The explanation is here (OOBasic looks to be the same as VB6 here)

http://stackoverflow.com/questions/2327 ... ing-in-vb6

Null is a type of variant. Objects can't be null
dim a as variant
a = null 'fine
dim b as object
b= null 'error - object variable not set
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Post by Herb40 »

Thanks for the info, JeJe.

I like Null more than Nothing. So I plan to change some of my Objects to Variants so I can set them to Null instead of Nothing. Andy Pitonyak says to use Variant, but I always figured that "the Basic system" would spend extra time trying to determine which flavor of Variant was being used.
OpenOffice 4.1.3 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Post by Villeroy »

Today's Basic is Python. Your office suite comes with a whole Python runtime as an alternative macro language with batteries included (lots of libraries for everyday's tasks). Python can be used for macros, stand-alone programs and for office extensions. In Python None is what Empty, Missing, Null and Nothing is in Basic. Python is by far easier to learn than Basic and much more powerful at the same time.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

Re: [Solved] Assigning NULL to an Object [Calc] Use NOTHING

Post by Herb40 »

Thanks, Villeroy, for taking the time to reply to my post and writing your suggestion that I should be using Python instead of Basic.

After a couple of years in the use of OpenOffice, I realize that I may have been better off starting with some language other than Basic. But with so much of the OO documentation suggesting Basic, and with the Basic IDE that works like a charm to an "old-tyme" programmer like me, I went with Basic, and now have a functional, sophisticated system all written in Basic. I appreciate all of the help I have received from this forum in making this possible.
OpenOffice 4.1.3 on Windows 10
Post Reply