asp classic - Error al buscar NULL en VBScript
asp-classic nullreferenceexception (3)
Tengo el siguiente VBScript en una página ASP clásica:
function getMagicLink(fromWhere, provider)
dim url
url = "magic.asp?fromwhere=" & fromWhere
If Not provider is Nothing Then '' Error occurs here
url = url & "&provider=" & provider
End if
getMagicLink = "<a target=''_blank'' href=''" & url & "''>" & number & "</a>"
end function
Sigo recibiendo un mensaje de error "Objeto requerido" en la línea que dice " If Not provider Is Nothing Then .
O bien el valor es NULL, o no es NULL, entonces, ¿por qué recibo este error?
Editar: Cuando invoco el objeto, paso NULL o paso una cadena.
Desde su código, parece que el provider es una variante o alguna otra variable, y no un objeto.
Is Nothing es solo para objetos, pero luego dices que es un valor que debería ser NULL o NOT NULL, que sería manejado por IsNull .
Intenta usar:
If Not IsNull(provider) Then
url = url & "&provider=" & provider
End if
Alternativamente, si eso no funciona, intente:
If provider <> "" Then
url = url & "&provider=" & provider
End if
Veo mucha confusión en los comentarios. Null , IsNull() y vbNull se utilizan principalmente para el manejo de bases de datos y normalmente no se utilizan en VBScript. Si no se establece explícitamente en la documentación del objeto / datos llamante, no lo use.
Para probar si una variable no está inicializada, use IsEmpty() . Para probar si una variable no está inicializada o contiene "" , prueba en "" o Empty . Para probar si una variable es un objeto, use IsObject y para ver si este objeto no tiene una prueba de referencia en Is Nothing .
En su caso, primero quiere probar si la variable es un objeto, y luego ver si esa variable es Nothing , porque si no es un objeto, obtiene el error "Object Required" cuando prueba en Nothing .
fragmento para mezclar y combinar en tu código:
If IsObject(provider) Then
If Not provider Is Nothing Then
'' Code to handle a NOT empty object / valid reference
Else
'' Code to handle an empty object / null reference
End If
Else
If IsEmpty(provider) Then
'' Code to handle a not initialized variable or a variable explicitly set to empty
ElseIf provider = "" Then
'' Code to handle an empty variable (but initialized and set to "")
Else
'' Code to handle handle a filled variable
End If
End If
Añadiré un espacio en blanco ("") al final de la variable y haré la comparación. Algo como a continuación debería funcionar incluso cuando esa variable sea nula. También puede recortar la variable solo en caso de espacios.
If provider & "" <> "" Then
url = url & "&provider=" & provider
End if