How to clean up null exception handling

Asked 2 years ago, Updated 2 years ago, 113 views

Usually when you make a code,

    if (someobject != null) {
        someobject.doCalc();
    }

They're making exceptions in the same way, but they look so dirty to me Is there a way to make it cleaner?

java nullpointerexception

2022-09-22 14:32

1 Answers

Each developer has a different way of handling exceptions, but if that way looks messy, Why don't we use the assert?

    assert someobject != null;

If you do this, when some object is null, an error is caused by the alert statement

But usually, if some object is not supposed to be null, for some processing,

    if( someobject == null ){
            //someobject processing
    }
    someobject.somemethod();

I think it would be good to do it like this


2022-09-22 14:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.