How to check NULL in IOS or Android !

A null indicates that a variable doesn’t point to any object and holds no value. You can use a basic ‘if’ statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something.

null is an object. Its type is object. null is a special value meaning “no value. undefined is not an object, it’s type is undefined.

The SQL NULL is the term used to represent a missing value. A NULL value in a table is a value in a field that appears to be blank. A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different than a zero value or a field that contains spaces.

An empty string, however, is a value – it is a string of no characters. Null is essentially ‘nothing’ – it’s the default ‘value’ (to use the term loosely) that Java assigns to any Object variable that was not initialized. Null isn’t really a value – and as such, doesn’t have properties.

NullPointerException is a RuntimeException . In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when an application attempts to use an object reference that has the null value. These include: Calling an instance method on the object referred by a null reference.

IOS

The NULL value for Objective-C objects (type id) is nil.

While NULL is used for C pointers (type void *).

  1. nil = (all lower-case) is a null pointer to an Objective-C object.
  2. Nil = (capitalized) is a null pointer to an Objective-C class.
  3. NULL = (all caps) is a null pointer to anything else (C pointers, that is).
  4. [NSNull null] = (singleton) for situations where use of nil is not possible (adding/receiving nil to/from NSArrays e.g.)

 

 

NSString* anyObjectorString = appDelegate.anyObjectorString;

if (anyObjectorString == nil || anyObjectorString == (id)[NSNull null])
{
// your Code here

} else
{
// Your code here
}

if ([appDelegate.anyObjectorString isKindOfClass:[NSNull class]])
{

//Your code is here

}

 

  1. #define SAFESTRING(str) ISVALIDSTRING(str) ? str : @””
  2. #define ISVALIDSTRING(str) (str != nil && [str isKindOfClass:[NSNull class]] == NO)
  3. #define VALIDSTRING_PREDICATE [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings) {return (BOOL)ISVALIDSTRING(evaluatedObject);}]

 

if (anyObjectorString == nil)
{
// do something
}

if (anyObjectorString == nil || [anyObjectorString isKindOfClass:[NSNull class]])
{
//do something
}
if (anyObjectorString == (id)[NSNull null] || anyObjectorString.length == 0 ) anyObjectorString = @"Something";

if([anyObjectorString length] >0)
{
// do something
}
if ( ( ![anyObjectorString isEqual:[NSNull null]] ) && ( [anyObjectorString length] != 0 ) ) {
}
if (anyObjectorString == NULL)
{
anyObjectorString = @"Photo uploaded...";
}
else if (getCaption == nil)
{
anyObjectorString = @"Photo uploaded...";
}
else if ([anyObjectorString isEqualToString:@""])
{
anyObjectorString = @"Photo uploaded...";
}
else if ([anyObjectorString isEqualToString:@" "])
{
anyObjectorString = @"Photo uploaded...";
}

ANDROID

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java this would be the equivalent of a NullPointerException or NPE for short.

An explicit call to throw NullPointerException();
Usage of the !! operator that is described below;

Attempts to access a member on a null reference of a platform type;

Generic types used for Java interoperation with incorrect nullability, e.g. a piece of Java code might add null into a Kotlin MutableList<String>, meaning that MutableList<String?> should be used for working with it;

Nullable types and Non-Null Types

if (userEmail != null && !userEmail.isEmpty()) {
}

String myString = null;

if (TextUtils.isEmpty(myString)) {
return; // or break, continue, throw
}
if (userEmail != null && !userEmail.isEmpty() && !userEmail.equals("null"))

if (userEmail.isNullOrEmpty())

if (userEmail.isNullOrBlank())

public static boolean isEmptyString(String text) {
return (text == null || text.trim().equals("null") || text.trim()
.length() <= 0);
}
public static boolean isEmpty(CharSequence str) {
if (str == null || str.length() == 0)
return true;
else
return false;
}
android:text='@{item.title != null ? user.title : ""}'

android:text='@{item.title ?? ""}'

if(!textview.getText().toString.matches(""))
{
// not null not empty
}else {
//null or empty
}
if (mText1.getText().length() > 0 && mText2.getText().length() > 0) {
// Your code.
}
if (textView.length() > 0) {
// textView is not null
} else {
// textView is null
}
String myString = null;

if (TextUtils.isEmpty(myString)) {
return; // or break, continue, throw
}
if (userEmail != null && !userEmail.isEmpty() && !userEmail.equals("null"))