TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Add detailed message to NullPointerException describing what is null

345 pointsby dhotsonabout 6 years ago

34 comments

cpetersoabout 6 years ago
When Firefox tried to make the &quot;X is undefined&quot; JavaScript exception message user-friendlier, it broke flipkart.com. The website&#x27;s JavaScript explicitly depended on the exact wording of exception messages. Simply loading the flipkart.com home page caused &quot;X is undefined&quot; exceptions, which it tried to parse with regular expressions. The new exception message had to be reverted. :(<p><a href="https:&#x2F;&#x2F;www.fxsitecompat.com&#x2F;en-CA&#x2F;docs&#x2F;2018&#x2F;improved-javascript-error-message-breaks-code-relying-on-the-legacy-format&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.fxsitecompat.com&#x2F;en-CA&#x2F;docs&#x2F;2018&#x2F;improved-javasc...</a><p>This is an unfortunate example of Hyrum&#x27;s Law: &quot;With a sufficient number of users of an API, all observable behaviors of your system will be depended on by somebody.&quot;<p><a href="http:&#x2F;&#x2F;www.hyrumslaw.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.hyrumslaw.com&#x2F;</a>
评论 #19494334 未加载
评论 #19494398 未加载
评论 #19493731 未加载
评论 #19493856 未加载
评论 #19494103 未加载
评论 #19502617 未加载
评论 #19493841 未加载
评论 #19494124 未加载
runarbergabout 6 years ago
In JavaScript I often find my self running the code in both firefox <i>and</i> chrome to get the full picture of <i>what is undefined</i>: (firefox)<p><pre><code> window.foo.bar &#x2F;&#x2F;=&gt; TypeError: window.foo is undefined </code></pre> and <i>what I&#x27;m trying to get</i>: (chrome)<p><pre><code> window.foo.bar &#x2F;&#x2F;=&gt; Uncaught TypeError: Cannot read property &#x27;bar&#x27; of undefined </code></pre> Personally I find the firefox error message more useful, but often I get a better understanding of whats wrong when I run it in chrome. I don’t know why they are mentioning undefined at all and don’t say something like:<p><pre><code> TypeError: Cannot read property &#x27;bar&#x27; of &#x27;window.foo&#x27; (undefined) </code></pre> ---<p>Edit: Formatting
评论 #19493987 未加载
评论 #19493587 未加载
评论 #19495538 未加载
Insanityabout 6 years ago
This sounds like a good addition.<p>It happens quite often that we get a bug report with &quot;We got a NPE&quot;. Our users often attach a screenshot, just showing the standard &quot;NPE error&quot; message, which does not really help.
评论 #19495912 未加载
evancox100about 6 years ago
&gt; Computation overhead &gt; NullPointerExceptions are thrown frequently.<p>&quot;Frequently&quot; is obviously a relative term, but are NullPointerExceptions really common enough to be a performance concern? It&#x27;s good that they are taking performance overhead into consideration of course, I&#x27;m just surprised it&#x27;s even an issue.
评论 #19493530 未加载
评论 #19493734 未加载
评论 #19493594 未加载
评论 #19493647 未加载
评论 #19495743 未加载
theandrewbaileyabout 6 years ago
This would be good, but I&#x27;d prefer that Java have something like the null operators that C# has.<p><a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;csharp&#x2F;language-reference&#x2F;operators&#x2F;null-conditional-operators" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;csharp&#x2F;language-refe...</a>
评论 #19493103 未加载
评论 #19493514 未加载
评论 #19493268 未加载
joemccall86about 6 years ago
This would be immensely beneficial, and long overdue in my opinion.
评论 #19497002 未加载
EmpirePhoenixabout 6 years ago
Funny thing, some JVM&#x27;s already do exactly this for many years:<p>Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: while trying to invoke the method de.hybris.platform.catalog.model.CatalogModel.getId() of a null object returned from de.hybris.platform.catalog.model.CatalogVersionModel.getCatalog();
评论 #19493614 未加载
评论 #19496180 未加载
js8about 6 years ago
I am not really that familiar with Java, but I don&#x27;t understand how to get a JVM heap dump if there is an exception that brings down the JVM or even a thread. Is it even possible?<p>I work on mainframe (z&#x2F;OS), and it is completely normal there that when an application fails with exception (they&#x27;re called ABENDs - from ABnormal ENDing), you can get a dump of memory of the application. From that, you can see all the values of the offending variables and all the relevant system areas and so on.
评论 #19493251 未加载
评论 #19493285 未加载
jayd16about 6 years ago
Can we add column numbers to the stack traces? Seems like that would solve this in the general case.
评论 #19494495 未加载
评论 #19496164 未加载
_Codemonkeyismabout 6 years ago
After 20 years, first in Java and then in Scala I&#x27;ve been tortured by this error message. Some years ago they fixed ClassCastException which for a long time also didn&#x27;t tell you what was wrong. There are some others if I remember correctly with bad error messages, like NumberFormatException. In general error messages on the JVM are very bad, wish they would take a look at Elm error messages.<p>I&#x27;ve left this year the JVM for TS, but glad for everyone who uses the JVM.
评论 #19494268 未加载
phinnaeusabout 6 years ago
If nothing else, it is really interesting to see exactly why this is hard.
评论 #19493257 未加载
yxhuvudabout 6 years ago
Better than nothing, but worse than having a type system than disallows the exception it in the first place.
评论 #19493407 未加载
gldev3about 6 years ago
I don&#x27;t know why i find this so funny but it&#x27;s definitely something that would be greatly appreciated!
edooabout 6 years ago
It is almost 2020 so it is reasonable to know what the actual error is.<p>This reminds me of old C&#x2F;C++ parsers that couldn&#x27;t tell you what line was expecting a bracket or semicolon and you got to manually find the parse error.
opportuneabout 6 years ago
This is awesome! I ran into this exact issue yesterday evening trying to debug a NPE on foo.bar().baz().hello()<p>IMO overhead should not even be a consideration. It doesn&#x27;t make sense to sacrifice detailed reporting just so someone can repeatedly handle NPEs as part of their application&#x27;s logic. I can&#x27;t imagine a situation in which I would look at code that throws enough NPEs for overhead to be a concern and say &quot;yes this is well designed code&quot;
kazinatorabout 6 years ago
This is too much of a hack; I predict it will be unreliable, at times sending programmers on wild goose chases more time-wasting than an uninformed investigation.
评论 #19493467 未加载
jolmgabout 6 years ago
I wonder if it&#x27;s possible to add something similar to the protection you get in Haskell to other languages via some static analyzer or something.<p>In Haskell, it&#x27;s pretty much impossible to have unexpected nulls because the ability to return nulls is something that is expressed in the type and needs to be handled before working with the possible type to be able to pass the type-check of the compiler.<p>For example, if I had a function:<p><pre><code> findFistOdd :: [Int] -&gt; Maybe Int </code></pre> That returned the first odd number in a list of numbers, the Maybe wraps the return value so the result is either `Nothing` or `Just someNumber`. I can&#x27;t work with the result directly like<p><pre><code> 1 + findFirstOdd [2,3] </code></pre> It would fail the type check, because I need to tell the compiler what the program should do if findFirstOdd couldn&#x27;t find an odd number. (1 +) is certainly not expecting a NULL as its argument type is Int. I can tell it, &quot;Just die if that happens&quot;:<p><pre><code> 1 + (fromMaybe (error &quot;couln&#x27;t find odd number&quot;) $ findFirstOdd [2,3]) </code></pre> or I can tell it to work on the inside value if it&#x27;s there, returning Just (1 + someOddNumber) if it&#x27;s there or Nothing if it&#x27;s not:<p><pre><code> (1 +) &lt;$&gt; findFirstOdd [2,3] </code></pre> Conversely, if a function says it returns an Int:<p><pre><code> addOne :: Int -&gt; Int </code></pre> That means it returns an Int, guaranteed. That&#x27;s never going to be some null value. It&#x27;s not something you&#x27;d ever have to consider.<p>Kind of wish all languages were like this. Nulls are probably the cause of most unexpected exceptions and it could be something that could always be caught without even running the program.
评论 #19494581 未加载
评论 #19494344 未加载
评论 #19494427 未加载
评论 #19494515 未加载
评论 #19494798 未加载
Myrmornisabout 6 years ago
&gt; Given the bytecode, it is not obvious which previous instruction pushed the null value. To find out about this, a simple data flow analysis is run on the bytecodes.<p>This is a petty nit on what sounds like some great work but please do not use the word &quot;simple&quot; in technical contexts like this. Having you assert that it is simple does not make it easier for anyone to understand, and it is really quite irritating. I don&#x27;t even know what &quot;data flow analysis&quot; is and I&#x27;ve been involved with computer programming and data analysis for 15 years.
评论 #19500214 未加载
hnthroaway1926about 6 years ago
Interesting, so they want to go beyond the line number of the exception and include information on what on the line is null.<p>It&#x27;s sort of funny that their example doesn&#x27;t follow the usual Java style guidelines of encapsulating member variables in the parent and using set() and get() methods for access.<p>a.to_b.to_c.to_d.num = 99;<p>This could be rewritten as a method in &#x27;A&#x27; as...<p>protected void setDNum(int num) {<p><pre><code> C c = b.getC(); D d = c.getD(); d.setNum(99); </code></pre> }<p>In this case, a nullpointerexception would include the line number (using default compiler options) clearly indicating which is null.
nickodellabout 6 years ago
&gt;As computing the NullPointerException message proposed here is a considerable overhead, this would slow down throwing NullPointerExceptions.<p>You could lower the overhead by not producing these messages if the exception is thrown frequently. It would be similar to the existing OmitStackTraceInFastThrow mechanism [0].<p>[0]: <a href="https:&#x2F;&#x2F;www.oracle.com&#x2F;technetwork&#x2F;java&#x2F;javase&#x2F;relnotes-139183.html" rel="nofollow">https:&#x2F;&#x2F;www.oracle.com&#x2F;technetwork&#x2F;java&#x2F;javase&#x2F;relnotes-1391...</a>
on_and_offabout 6 years ago
I don&#x27;t see that many NPE in kotlin these days but pretty sure that on java&#x2F;Android I get a useful error message.<p>Is that an ART&#x2F;Dalvik feature ?
评论 #19493452 未加载
评论 #19492982 未加载
评论 #19492983 未加载
jimmaswellabout 6 years ago
Meanwhile, for as long as I can remember, Visual Studio could easily jump straight to the offending null variable when debugging C#.
评论 #19493087 未加载
评论 #19493307 未加载
IloveHN84about 6 years ago
Java is pretty weird land. It&#x27;s 2019 and there&#x27;s still this issue with Nullpointer Exception and the other big limitation is that you cannot create a new instance of a generic type in a generic function, making programs dynamic, like in C++. Funny, because the whole JVM is based on C++
rb808about 6 years ago
I&#x27;m a bit torn on this because I always avoided having too many method calls on the same line. Code Complete recommended this a long time ago.<p>However now I see just about everyone stacking this method calls one after the other. Is this just acceptable now?<p>state = stateFromPostcode(customer.getAddress().getPostCode());
评论 #19493846 未加载
评论 #19493100 未加载
评论 #19493461 未加载
评论 #19493173 未加载
评论 #19495586 未加载
评论 #19493210 未加载
_ZeD_about 6 years ago
this is good stuff. please come back 10 years ago and do it!
评论 #19494977 未加载
评论 #19493402 未加载
4thaccountabout 6 years ago
I don&#x27;t write Java, but support an application written in Java that gets null pointer issues and aborts all the time. It&#x27;s also annoying that there is precious little information for me to go on.
评论 #19493605 未加载
评论 #19493397 未加载
评论 #19493768 未加载
goshxabout 6 years ago
Finally someone noticed it!
rainhackerabout 6 years ago
But they are still calling it NullPointerException, not NullReferenceException. I guess this change is too invasive
评论 #19496390 未加载
mark242about 6 years ago
Better idea:<p><pre><code> Optional.ofNullable(foo).orElse(new Foo())</code></pre>
评论 #19493699 未加载
评论 #19493655 未加载
portal_narlishabout 6 years ago
JEP draft: Remove NullPointerException from language
kordsabout 6 years ago
hopefully we&#x27;ll see the same thing in &quot;Object reference not set to an instance of an object&quot; exceptions in .NET
danraabout 6 years ago
I hope I&#x27;m not the only one who read the title as adding some helpful text to the exception for n00bs, which explains about the very concept of null.
jackvezkovicabout 6 years ago
That moment when you step out of your safe Scala world just to realize that people still face NPEs.
评论 #19493527 未加载
jtdevabout 6 years ago
Other than supporting and working with legacy software, I cannot muster any reason to write code in Java at this point... it’s just a terrible language to work with.
评论 #19495570 未加载
评论 #19493291 未加载
评论 #19493169 未加载