How can I check if a variable is Null with AutoLisp?

How can I check if a variable is Null with AutoLisp?

Anonymous
Not applicable
9,089 Views
3 Replies
Message 1 of 4

How can I check if a variable is Null with AutoLisp?

Anonymous
Not applicable

How can I check if a variable is Null with AutoLISP? I have tried a dozen different methods and nothing seems to work for this simple script. 

 

What I am trying to do is see if variable "a" has a value and if it does not then prompt the user to assign it. Here are some of the methods I have tried so far...

 

(defun c:ZOINK ()
    (if (null (getvar a))
    (if (null a))
    (if (nil a))
    (if (= a nil))
    (if (/= a nil))
    (if (= a null))
    (if (/= a null))    
        (setq a (getreal "\nEnter a Number : ")
         b (getreal "\nEnter Second Number : ")
        )
    )
)
 
Every single one returns with an AutoCAD message stating "AutoCAD variable setting rejected: "CMDECHO" nil". If I remove the conditional the script works as expected.
0 Likes
Accepted solutions (1)
9,090 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

Are you talking about variables in general, or autocad's system variables? Because (getvar a) you can only use for 2nd ones.

 

Also, it difference between 0 and nil. The CMDECHO system variable accepts 0 or 1 values. You cannot assign nil to any system variables.

 

If a is a regular variable, there are many ways not to test if it's nil (= null). 

(defun c:ZOINK ()
  (if (not a)
    (setq a (getreal "\nEnter a Number: ")
          b (getreal "\nEnter Second Number: "))))

Or very often we use OR.

(or a
    (setq a (getreal "\nEnter a Number: ")
          b (getreal "\nEnter Second Number: ")))

 

These are also possible

(null a); (= nil a); (/= T a) 

 

BUT compare to (zerop a) - This tests whether a is actual 0 (not nil).

0 Likes
Message 3 of 4

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

....

    (if (null (getvar a)) wrong; a has to be a name of system varible - (getvar "cmdecho"); also never nil, so usually (if (= (getvar "cmdecho") 0), or (zerop (getvar "cmdecho"))
    (if (null a)) good
    (if (nil a)) wrong, nil is a symbol
    (if (= a nil)) good
    (if (/= a nil)) good, but it the other way around
    (if (= a null)) wrong, null is the function, not a symbol
    (if (/= a null))  wrong, dtto  
        (setq a (getreal "\nEnter a Number : ")
         b (getreal "\nEnter Second Number : ")
        )
    )
)
 
....

 

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

    (if (null (getvar a))
    (if (null a)) ; get rid of extraneous )
    (if (nil a))
    (if (= a nil)) ; get rid of extraneous )
    (if (/= a nil))
    (if (= a null))
    (if (/= a null))
....

or, as already suggested:

 

(if (not a)

 

This one:

 

(if (/= a nil)

 

[without the extraneous ) ]

 

can be done with simply:

 

(if a

 

Kent Cooper, AIA
0 Likes