简体   繁体   中英

How check if an Attribute(object) is null in Java

I need specific data for a report, then I gettin all information from a parent object

Object1

It has many attributes, object attributes

Object11, Object12, Object13, attr1, attr2...

The attributes has many attributes too

Object111, Object131, Object132,..

by now I got 5 level data attributes.

When I send information to my report it says, Error: cause:null

object1.getIdObject11().getIdObject111().getDescription;

It trows error because Object111 is null

I tried using

object1.getIdObject11().getIdObject111().getDescription==null?'':object1.getIdObject11().getIdObject111().getDescription;

but it only verify if description is null, and throws the same error Then I tried to verify Object

if(object1.getIdObject11().getIdObject111() == null) {
    var = object1.getIdObject11().getIdObject111().getDescription;
} else {
    var = "";
}

But when Object11 is null, it throws same error.

I don't think its a good way doing this for each attribute (have to get like 30 attributes)

if(object1.getIdObject11()!=null) {
    if(object1.getIdObject11().getIdObject111()!=null) {
        if(object1.getIdObject11().getIdObject111().getIdObject1111()!=null) {
             //...
         }
    }
}

I want to verify if is there a null object and set '' (blank) if it is, with no such a large code(because the gotten params are set inside a report, mixed with letter).

reportline1 = "Area: "+object1.getIdObject11().getIdObject111().getName;

You code breaks Demeter's law . That's why it's better to refactor the design itself. As a workaround, you can use Optional

   var = Optional.ofNullable(object1)
    .map(o -> o.getIdObject11())
    .map(o -> o.getIdObject111())
    .map(o -> o.getDescription())
    .orElse("")

The way I would probably do this to extend the functionality of the code easily in the future might take a bit of writing in the beginning but will be easily usable forever.

I would create a new method in your parent class called hasNull that returns a boolean like so:

public boolean hasNull()
{
    boolean hasANull = false;

    //Call another hasNull() inside of object11 which in turns calls hasNull() in object111 etc.

    //If any of the calls return with a true/null value set hasANull to true
    return hasANull;  
}

This in turn checks to see if the current objects it contains are null . If one of the class variables is another custom class you created you can then add another hasNull into that one and keep going until you get to the lowest level where you can do a specific operation when the value is null such as set it to "" .

After implementing this you will be able to just be able to use it like this any time you need it:

if (!object1.hasNull())
{
//Do whatever you want if there are no null values
}
else
{
//Do whatever you want if there is a null value
}

You can also make this a void method if you only want it to toggle the values on the lowest level, and do not need to do anything in either case.

I prefer the solution that gave dehasi.

But you can also do something like that:

getOrElse(() -> object1.getIdObject11().getIdObject111().getDescription(), "")

Where getOrElse is:

public static <T> T getOrElse(Supplier<T> getter, T elseValue) {
    try {
        return getter.get();
    } catch (Exception e) {
        // log or do something with it
    }
    return elseValue;
}

It may be controversial becaouse you use Exception to do this.

You can use this code to check if your object has a null attribute, the object is myclass ;

for (Field f : myclass.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                try {
                    if (Objects.isNull(f.get(myclass))) {
                        isLineContainsNull = true;
                    }
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM