In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to avoid NullPointerException errors in Java". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to avoid NullPointerException errors in Java" can help you solve your doubts.
I object! = null to avoid a lot of NullPointerException.
What are the alternatives:
If (someobject! = null) {someobject.doCalc ();} solution:
It seems to me that this is a fairly common problem that junior and mid-level developers tend to encounter at some point: they either don't know or don't trust the contracts they're involved in, and check null defensively. In addition, when writing their own code, they tend to rely on returning null values to represent something, so the caller is required to check for null values.
In other words, a null check occurs in two cases:
If null, it represents a valid reply in the contract; and
If it's not a valid response.
(2) easy. Use assert statements (assertions) or allow failures (such as NullPointerException). Assertion is an unwidely used Java feature added in 1. 4. The syntax is:
Assert
Or
Assert:
Where is a Boolean expression, is an object, and the output of its toString () method is included in the error.
An assert statement throws an Error (AssertionError if the condition is incorrect). By default, Java ignores assertions. You can enable assertions by passing the option-ea to JVM. You can enable and disable assertions for individual classes and packages. This means that although my tests show little impact of assertions on performance, you can use assertions to validate code during development and testing and disable them in a production environment.
In this case, it is OK not to use assertions, because the code will only fail, which is what happens when assertions are used. The only difference is that with an assertion, it may happen earlier, in a more meaningful way, and may contain additional information, which can help you figure out why it was unexpected.
(1) it is a bit difficult. If you have no control over the code you are calling, you will be in trouble. If the null is a valid response, you must check it.
However, if it's the code you control (which is usually the case), that's another matter. Avoid using null as a response. It's easy to use the method of returning a collection: it almost always returns an empty collection (or array) instead of null.
Using non-collections can be more difficult. Take this as an example: if you have the following interfaces:
Public interface Action {void doSomething ();} public interface Parser {Action findAction (String userInput);}
In Parser, the original user input will find the action to be performed, perhaps when you implement the command-line interface of a function. Now, without proper action, you can return the contract to null. This will lead to the empty check you are talking about.
Another solution is to never return null and instead use Null Object mode:
Public class MyParser implements Parser {private static Action DO_NOTHING = new Action () {public void doSomething () {/ * do nothing * /}}; public Action findAction (String userInput) {/ /. If (/ * we can't find any actions * /) {return DO_NOTHING;}
Compare:
Parser parser = ParserFactory.getParser (); if (parser = = null) {/ / now what? / / this would be an example of where null isn't (or shouldn't be) a valid response} Action action = parser.findAction (someInput); if (action = = null) {/ / do nothing} else {action.doSomething ();}
To
ParserFactory.getParser (). FindAction (someInput). DoSomething ()
This is a better design because it can lead to more concise code.
That is, it is perfectly appropriate for the findAction () method to throw an Exception exception with a meaningful error message-especially in this case, where you rely on user input. Throwing an exception for a findAction method is much better than a calling method that throws an unexplained simple NullPointerException.
Try {ParserFactory.getParser () .findAction (someInput) .doSomething ();} catch (ActionNotFoundException anfe) {userConsole.err (anfe.getMessage ());}
Or, if you think the try / catch mechanism is too ugly to do nothing, your default action should provide feedback to the user.
Public Action findAction (final String userInput) {/ * Code to return requested Action if found * / return new Action () {public void doSomething () {userConsole.err ("Action not found:" + userInput) After reading this, the article "how to avoid NullPointerException errors in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.