The problem is your checking for a sprite collision between sprites 2 and 3, yet sprite 2 is not created until you press the space bar, thats why you get the error.
Your code should be (note I've cleaned it up a bit)
sync on
load bitmap "ship.bmp",1
Get image 1,0,0,24,24
load bitmap "bullet.bmp",1
get image 2,0,0,24,24
load bitmap "alien.bmp",1
get image 3,0,0,24,24
pos#=240
bullet#=351
hide mouse
do
Sprite 1,pos#,375,1
sprite 3,pos#,24,3
if rightkey()=1
pos#=pos#+10
endif
if leftkey()=1
pos#=pos#-10
endif
if spacekey()=1
bullet#=351
repeat
sprite 2,pos#,bullet#,2
bullet#=bullet#-10
sync
until bullet#<-30
endif
if sprite exist(2) = 1
if sprite collision(2,3)>0 then hide sprite 3
endif
sync
loop
or instead of checking if the sprite exists you could create it before hand like so
sync on
load bitmap "ship.bmp",1
Get image 1,0,0,24,24
load bitmap "bullet.bmp",1
get image 2,0,0,24,24
load bitmap "alien.bmp",1
get image 3,0,0,24,24
sprite 2,pos#,bullet#,2
hide sprite 2
pos#=240
bullet#=351
hide mouse
do
Sprite 1,pos#,375,1
sprite 3,pos#,24,3
if rightkey()=1
pos#=pos#+10
endif
if leftkey()=1
pos#=pos#-10
endif
if spacekey()=1
bullet#=351
show sprite 2
repeat
sprite 2,pos#,bullet#,2
bullet#=bullet#-10
sync
until bullet#<-30
endif
if sprite collision(2,3)>0 then hide sprite 3
sync
loop