6 ms·
I can't believe it took this long to show the name of null references. NullReferenceException is actually the first exception I ever encountered when I started
by dimgl 10y ago
I can't believe it took this long to show the name of null references. NullReferenceException is actually the first exception I ever encountered when I started learning how to use C#.
That being said, better late than never.
- iamcreasy 10y agoAnybody knows if/when Java will have this feature?
- meddlepal 10y agoI doubt believe it will ever or at least not anytime soon. I don't think the JVM preserves identifier names in the bytecode in a way that it can be accessed easily. Though I do recall a long time ago reading about some JVM switches that allow that data to be accessed but with performance issues. Can't recall where I read that.
- kodfodrasz 10y agoSadly (as one developing a lot in Java) i think it won't happen anytime soon, and the community will ideologize it as "it is even better that we don't have it". :( I hope I will be wrong. ps: a language where non-nullable references are the thing would be the best.
- lmm 10y agoJava has finally got options, so at least it's theoretically possible to start a migration path to non-nullable. It will take decades though.
- kodfodrasz 10y agoI tried to use the java Optional<T>, but the API has some points, which make it inconvenient for some usecases. (Some of these are addressed in Java 9) As I see: unfortunately without algebraic types you cannot solve this properly, even if you address the lacking points in Optional<T>
- benjiweber 10y agoSAP's JVM implementation has NullPointerExceptions that tell you where the null originated from, which is neat. See slide 17 of https://oracleus.activeevents.com/2014/connect/fileDownload/session/49D67EA3D0EF2126B5B85D06522202C0/CON3243_Simonis.pdf https://oracleus.activeevents.com/2014/connect/fileDownload/...
- ygra 10y agoIs that really such a sorely missed feature? I tend to need just a quick glance at the offending line to isolate potential null references and in more severe cases just check the tool tips of expressions. Or are others writing code that's unusually dereference-heavy?
- dimgl 10y agoOnce you're experienced and writing better code, this isn't a crucial feature. It does help when you've got coworkers who write classes with 20,000 lines however (not an exaggeration, I've actually seen this).
- niftich 10y agoI'd imagine that with the rise of the 'fluent' API style of foo.bar().baz().doThis().doThat(toThis).doAnother(toThat); the number of potential null-references per line has gone up from the days when we were all writing code with mutator methods that returned void.
- gnaritas 10y agoThe Fluent API styles is to return "this" so setter methods can be chained, that style doesn't produce nulls.
- AndrewDucker 10y agoIt does when it has a bug in it...
- niftich 10y agoYes, but any of the passed-in arguments can be null if they are de-referenced without checking, or you can muck up your context by doing something weird. I'm not justifying buggy code, but these days we do handle more objects-per-debug-line-number than we used to.
- tracker1 10y ago
- jpalomaki 10y ago+1 for this. The null conditional is also pretty nice thing for the chained method calls. In C# you can write like this: int? count = customers?[0]?.Orders?.Count(); https://msdn.microsoft.com/fi-fi/library/dn986595.aspx https://msdn.microsoft.com/fi-fi/library/dn986595.aspx Pragmatic features like this are one reason why I love C#. I get the feeling that language designers are actually looking at real world code and thinking what kind of features would be it better.
- piaste 10y agoSlight aside: please don't abuse the Elvis operator like that (by using it on every single nullable reference), it could easily grow into a widespread anti-pattern that makes debugging harder. My rule of thumb is that ?. is OK to use when 1) The reference is expected to occasionally be null in the course of normal operations 2) You legitimately don't care if the reference is unexpectedly null, as you wouldn't or couldn't fix it even if you knew about it Otherwise it's better to discover and fix the NullReferenceException ASAP.
- perezdev 10y agoIt's the null propagation operator.
- breischl 10y agoLook, if you want to be pedantic it's the null conditional operator[1]. But it is widely called the "Elvis operator"[2]. [1] - https://msdn.microsoft.com/en-us/library/dn986595.aspx https://msdn.microsoft.com/en-us/library/dn986595.aspx [2] - https://www.google.com/#q=c%23+elvis+operator https://www.google.com/#q=c%23+elvis+operator
- gregmac 10y agoGenerally agreed, and out of curiosity I just looked through some fairly new code I have that's pretty heavy on C# 6, and only found a single instance of two null conditionals being used: Assert.That(r.Exception?.InnerException?.Message, Is.EqualTo("Exception result")); Next closest thing that shows up a few times is coalescing to a default value: config?.OutputName ?? DefaultConfig.OutputName Some other examples that show up a lot: Invoke Action<> or Func<> that may be null or unassigned (this works for Events as well): actionHandler?.Invoke(value); Extracting a value from a LINQ expression on a list of container objects (this one is actually from a unit test where it's explicitly only expecting one item): items.First()?.Data Easy way to do a Coalesce on a specific property value in a collection: items.FirstOrDefault(item => item.Data != null)?.Data