Converting a Date object to a calendar object [duplicate]

Here’s your method: public static Calendar toCalendar(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } Everything else you are doing is both wrong and unnecessary. BTW, Java Naming conventions suggest that method names start with a lower case letter, so it should be: dateToCalendar or toCalendar (as shown). OK, let’s milk your code, shall … Read more

Android – How To Override the “Back” button so it doesn’t Finish() my Activity?

Remove your key listener or return true when you have KEY_BACK. You just need the following to catch the back key (Make sure not to call super in onBackPressed()). Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification … Read more

Best explanation for languages without null

I think the succinct summary of why null is undesirable is that meaningless states should not be representable. Suppose I’m modeling a door. It can be in one of three states: open, shut but unlocked, and shut and locked. Now I could model it along the lines of class Door private bool isShut private bool … Read more

No Exception while type casting with a null in java

You can cast null to any reference type without getting any exception. The println method does not throw null pointer because it first checks whether the object is null or not. If null then it simply prints the string “null”. Otherwise it will call the toString method of that object. Adding more details: Internally print … Read more

Why use Optional.of over Optional.ofNullable?

Your question is based on assumption that the code which may throw NullPointerException is worse than the code which may not. This assumption is wrong. If you expect that your foobar is never null due to the program logic, it’s much better to use Optional.of(foobar) as you will see a NullPointerException which will indicate that … Read more

Why can I throw null in Java? [duplicate]

It looks like it’s not that null is treated as a NullPointerException, but that the act of attempting to throw null itself throws a NullPointerException. In other words, throw checks that its argument is nonnull, and if it is null, it throws a NullPointerException. JLS 14.18 specifies this behavior: If evaluation of the Expression completes … Read more

Why should one use Objects.requireNonNull()?

Because you can make things explicit by doing so. Like: public class Foo { private final Bar bar; public Foo(Bar bar) { Objects.requireNonNull(bar, “bar must not be null”); this.bar = bar; } Or shorter: this.bar = Objects.requireNonNull(bar, “bar must not be null”); Now you know: when a Foo object was successfully created using new() then … Read more

NullPointerException in Java with no StackTrace

You are probably using the HotSpot JVM (originally by Sun Microsystems, later bought by Oracle, part of the OpenJDK), which performs a lot of optimization. To get the stack traces back, you need to pass the option -XX:-OmitStackTraceInFastThrow to the JVM. The optimization is that when an exception (typically a NullPointerException) occurs for the first … Read more

NullPointerException in Collectors.toMap with null entry values

You can work around this known bug in OpenJDK with this: Map<Integer, Boolean> collect = list.stream() .collect(HashMap::new, (m,v)->m.put(v.getId(), v.getAnswer()), HashMap::putAll); It is not that much pretty, but it works. Result: 1: true 2: true 3: null (this tutorial helped me the most.) EDIT: Unlike Collectors.toMap, this will silently replace values if you have the same … Read more

tech