-2

My confined to Java features that will work inside the tMap component of Talend Open Studio.
I hate dealing with NullPointerExceptions. I have to clutter my code often with checks instead of just a.equals(b).

I created some sample functions and realized I can't be the only person who thought of this:

public static Integer toInt(Double d){
    return (d == null) ? 0 : d.intValue();
}

public static Integer toInt(String d){
    return (d == null) ? 0 : Integer.parseInt(d);
}

// equals without nullpointer exceptions
public static Boolean eq(Object a, Object b){
    // no nullpointerexception
    if (a == null && b == null) { return true;}
    if (a == null ^ b == null) { return false;}
    return a.equals(b);
}

Should my project keep accumulating such general methods, which many others probably need as well, or can I use an existing library as a dependency? I mean a lib of convenience methods in Java that just work.

Darian Hickman
  • 853
  • 10
  • 26
  • 2
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – CubeJockey Sep 18 '15 at 14:08
  • 1
    Maybe the real problem is you shouldn't pass null to these methods. – Manu Sep 18 '15 at 14:08
  • possible duplicate of http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java – Manu Sep 18 '15 at 14:09
  • The proposed duplicate is a very different question. – Marko Topolnik Sep 18 '15 at 14:15
  • BTW your return types are probably wrong. You want primitive types instead. – Marko Topolnik Sep 18 '15 at 14:16
  • @MarkoTopolnik OP says *"I hate dealing with NullPointerExceptions"*. That's what the linked question is about. – Manu Sep 18 '15 at 14:17
  • @manu OP's question is about utility functions _dealing_ with nulls, not about passing/returning nulls. You cannot pretend to know that OP can choose to avoid dealing with nulls. – Marko Topolnik Sep 18 '15 at 14:18
  • @MarkoTopolnik Are you implying OP is not searching for a library? – Manu Sep 18 '15 at 14:39
  • @Manu I am implying that OP, in addition to asking about a library, has described his problem and shown what he has done so far. Closing a question with a reason which states precisely that advice on how to improve the question is wrong. – Marko Topolnik Sep 18 '15 at 14:41
  • 2
    IMO, if you have to do a lot of null checking in your code, it suggests to me there is a pervasive design problem in either >>your<< code, or the code of the APIs you are programming against / using. – Stephen C Sep 18 '15 at 14:41

1 Answers1

2

There are many such libraries, especially from Apache (ObjectUtils may match your presented need). The actual problem with all these is the reverse: each library offers some set of functions and it never matches your desired set. So you end up increasing the bulk of your app with unused code.

Usually no single project will need very many of these utility methods so a better approach is to either write them as you started, or grab the code you need from open-source libraries.

Also, be sure to keep up with the improvements in JDK itself. For example, you may not be aware of java.util.Objects yet.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436