Quantcast
Channel: Check two arguments in Java, either both not null or both null elegantly - Stack Overflow
Browsing all 15 articles
Browse latest View live

Answer by Mohammad Kholghi for Check two arguments in Java, either both not...

if ((from == null) == (password == null)){ //if both are true or both are false}(Source: Intellij IDEA)

View Article



Answer by Reinstate Monica for Check two arguments in Java, either both not...

I'm surprised nobody mentioned the simple solution of making from and password fields of a class and passing a reference to an instance of that class:class Account { final String name, password;...

View Article

Answer by gbronner for Check two arguments in Java, either both not null or...

Nobody seems to have mentioned the ternary operator:if (a==null? b!=null:b==null)Works nicely for checking this particular condition, but doesn't generalize well past two variables.

View Article

Answer by SQB for Check two arguments in Java, either both not null or both...

Since you want to do something special (use defaults) when both sender and password are absent, handle that first.After that, you should have both a sender and a password to send an e-mail; throw an...

View Article

Answer by Arnab Datta for Check two arguments in Java, either both not null...

Here's a relatively straight-forward way that does not involve any Xor og lengthy ifs. It does however require you to be slightly more verbose, but on the upside, you can use the custom Exceptions I...

View Article


Answer by Khaled.K for Check two arguments in Java, either both not null or...

Here is a general solution for any number of null checkspublic static int nulls(Object... objs){ int n = 0; for(Object obj : objs) if(obj == null) n++; return n;}public static void main (String[] args)...

View Article

Answer by Didier L for Check two arguments in Java, either both not null or...

A Java 8 solution would be to use Objects.isNull(Object), assuming a static import:if (isNull(from) != isNull(password)) { throw ...;}For Java < 8 (or if you don't like using Objects.isNull()), you...

View Article

Answer by x4u for Check two arguments in Java, either both not null or both...

I would like to suggest another alternative which is how I would actually write this piece of code:if( from != null ){ if( password == null ) error( "password required for "+ from );}else{ if( password...

View Article


Answer by matt for Check two arguments in Java, either both not null or both...

I think a correct way to handle this is to consider three situations: both 'from' and 'password' are provided, neither are provided, a mix of the two are provided.if(from != null && password !=...

View Article


Answer by Stig Hemmer for Check two arguments in Java, either both not null...

Personally, I prefer readable to elegant.if (from != null && password == null) { throw new RuntimeException("-from given without -password");}if (from == null && password != null) {...

View Article

Answer by ASA for Check two arguments in Java, either both not null or both...

Put that functionality in a 2 argument method with the signature:void assertBothNullOrBothNotNull(Object a, Object b) throws RuntimeExceptionThis saves space in the actual method you are interested in...

View Article

Answer by JordiVilaplana for Check two arguments in Java, either both not...

As I see your intentions, there is no need to always check both exclusive nullities but to check if password is null if and only if from is not null. You can ignore the given password argument and use...

View Article

Answer by coolguy for Check two arguments in Java, either both not null or...

There is a way using the ^ (XOR) operator:if (from == null ^ password == null) { // Use RuntimeException if you need to throw new IllegalArgumentException("message");}The if condition will be true if...

View Article


Answer by Jon Skeet for Check two arguments in Java, either both not null or...

Well, it sounds like you're trying to check whether the "nullity" condition of the two is the same or not. You could use:if ((from == null) != (password == null)){ ...}Or make it more explicit with...

View Article

Check two arguments in Java, either both not null or both null elegantly

I used spring boot to develop a shell project used to send email, e.g.sendmail -from foo@bar.com -password foobar -subject "hello world" -to aaa@bbb.comIf the from and password arguments are missing, I...

View Article

Browsing all 15 articles
Browse latest View live




Latest Images