每次拿到对象在进行相关方法或属性的时候都要进行如下操作
先判断是否为null
if(obj!=null){
//todo.......
}else{
//todo
}
这样的代码,既繁琐,又影响阅读性,而且一不小心就会报如下错误信息
D:\Java\jdk1.8.0_202\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=55047:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_202\jre\lib\charsets.jar;D:\Java\jdk1.8.0_202\jre\lib\deploy.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_202\jre\lib\javaws.jar;D:\Java\jdk1.8.0_202\jre\lib\jce.jar;D:\Java\jdk1.8.0_202\jre\lib\jfr.jar;D:\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_202\jre\lib\jsse.jar;D:\Java\jdk1.8.0_202\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_202\jre\lib\plugin.jar;D:\Java\jdk1.8.0_202\jre\lib\resources.jar;D:\Java\jdk1.8.0_202\jre\lib\rt.jar;D:\nethospital\ProdDev_BackEnd\hospital-service\target\classes;D:\Java\repositories\org\springframework\boot\spring-boot-starter\2.1.3.RELEASE\spring-boot-starter-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot\2.1.3.RELEASE\spring-boot-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\spring-context\5.1.5.RELEASE\spring-context-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-autoconfigure\2.1.3.RELEASE\spring-boot-autoconfigure-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-logging\2.1.3.RELEASE\spring-boot-starter-logging-2.1.3.RELEASE.jar;D:\Java\repositories\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Java\repositories\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Java\repositories\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\Java\repositories\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\Java\repositories\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Java\repositories\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Java\repositories\org\springframework\spring-core\5.1.5.RELEASE\spring-core-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-jcl\5.1.5.RELEASE\spring-jcl-5.1.5.RELEASE.jar;D:\Java\repositories\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-config\2.1.1.RELEASE\spring-cloud-starter-config-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter\2.1.1.RELEASE\spring-cloud-starter-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-context\2.1.1.RELEASE\spring-cloud-context-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\security\spring-security-rsa\1.0.7.RELEASE\spring-security-rsa-1.0.7.RELEASE.jar;D:\Java\repositories\org\bouncycastle\bcpkix-jdk15on\1.60\bcpkix-jdk15on-1.60.jar;D:\Java\repositories\org\bouncycastle\bcprov-jdk15on\1.60\bcprov-jdk15on-1.60.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-config-client\2.1.1.RELEASE\spring-cloud-config-client-2.1.1.RELEASE.jar;D:\Java\repositories\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Java\repositories\com\fasterxml\jackson\core\jackson-databind\2.9.8\jackson-databind-2.9.8.jar;D:\Java\repositories\com\fasterxml\jackson\core\jackson-core\2.9.8\jackson-core-2.9.8.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-netflix-eureka-client\2.1.1.RELEASE\spring-cloud-starter-netflix-eureka-client-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-netflix-hystrix\2.1.1.RELEASE\spring-cloud-netflix-hystrix-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-aop\2.1.3.RELEASE\spring-boot-starter-aop-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-netflix-eureka-client\2.1.1.RELEASE\spring-cloud-netflix-eureka-client-2.1.1.RELEASE.jar;D:\Java\repositories\com\netflix\eureka\eureka-client\1.9.8\eureka-client-1.9.8.jar;D:\Java\repositories\org\codehaus\jettison\jettison\1.3.7\jettison-1.3.7.jar;D:\Java\repositories\stax\stax-api\1.0.1\stax-api-1.0.1.jar;D:\Java\repositories\com\netflix\netflix-commons\netflix-eventbus\0.3.0\netflix-eventbus-0.3.0.jar;D:\Java\repositories\com\netflix\netflix-commons\netflix-infix\0.3.0\netflix-infix-0.3.0.jar;D:\Java\repositories\commons-jxpath\commons-jxpath\1.3\commons-jxpath-1.3.jar;D:\Java\repositories\joda-time\joda-time\2.10.1\joda-time-2.10.1.jar;D:\Java\repositories\org\antlr\antlr-runtime\3.4\antlr-runtime-3.4.jar;D:\Java\repositories\org\antlr\stringtemplate\3.2.1\stringtemplate-3.2.1.jar;D:\Java\repositories\antlr\antlr\2.7.7\antlr-2.7.7.jar;D:\Java\repositories\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;D:\Java\repositories\org\apache\commons\commons-math\2.2\commons-math-2.2.jar;D:\Java\repositories\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\Java\repositories\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\Java\repositories\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\Java\repositories\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\Java\repositories\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\Java\repositories\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\Java\repositories\org\apache\httpcomponents\httpclient\4.5.7\httpclient-4.5.7.jar;D:\Java\repositories\org\apache\httpcomponents\httpcore\4.4.11\httpcore-4.4.11.jar;D:\Java\repositories\com\google\inject\guice\4.1.0\guice-4.1.0.jar;D:\Java\repositories\javax\inject\javax.inject\1\javax.inject-1.jar;D:\Java\repositories\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\Java\repositories\com\github\vlsi\compactmap\compactmap\1.2.1\compactmap-1.2.1.jar;D:\Java\repositories\com\github\andrewoma\dexx\dexx-collections\0.2\dexx-collections-0.2.jar;D:\Java\repositories\com\netflix\eureka\eureka-core\1.9.8\eureka-core-1.9.8.jar;D:\Java\repositories\org\codehaus\woodstox\woodstox-core-asl\4.4.1\woodstox-core-asl-4.4.1.jar;D:\Java\repositories\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.1.1.RELEASE\spring-cloud-starter-netflix-archaius-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-netflix-archaius\2.1.1.RELEASE\spring-cloud-netflix-archaius-2.1.1.RELEASE.jar;D:\Java\repositories\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\Java\repositories\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.1.1.RELEASE\spring-cloud-starter-netflix-ribbon-2.1.1.RELEASE.jar;D:\Java\repositories\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\Java\repositories\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\Java\repositories\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\Java\repositories\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\Java\repositories\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\Java\repositories\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\Java\repositories\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\Java\repositories\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\Java\repositories\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\Java\repositories\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\Java\repositories\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\Java\repositories\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\Java\repositories\com\netflix\ribbon\ribbon-eureka\2.3.0\ribbon-eureka-2.3.0.jar;D:\Java\repositories\com\thoughtworks\xstream\xstream\1.4.10\xstream-1.4.10.jar;D:\Java\repositories\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;D:\Java\repositories\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-actuator\2.1.3.RELEASE\spring-boot-starter-actuator-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-actuator-autoconfigure\2.1.3.RELEASE\spring-boot-actuator-autoconfigure-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-actuator\2.1.3.RELEASE\spring-boot-actuator-2.1.3.RELEASE.jar;D:\Java\repositories\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.8\jackson-datatype-jsr310-2.9.8.jar;D:\Java\repositories\io\micrometer\micrometer-core\1.1.3\micrometer-core-1.1.3.jar;D:\Java\repositories\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\Java\repositories\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-netflix-hystrix\2.1.1.RELEASE\spring-cloud-starter-netflix-hystrix-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-netflix-ribbon\2.1.1.RELEASE\spring-cloud-netflix-ribbon-2.1.1.RELEASE.jar;D:\Java\repositories\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\Java\repositories\com\netflix\hystrix\hystrix-serialization\1.5.18\hystrix-serialization-1.5.18.jar;D:\Java\repositories\com\fasterxml\jackson\module\jackson-module-afterburner\2.9.8\jackson-module-afterburner-2.9.8.jar;D:\Java\repositories\com\netflix\hystrix\hystrix-metrics-event-stream\1.5.18\hystrix-metrics-event-stream-1.5.18.jar;D:\Java\repositories\com\netflix\hystrix\hystrix-javanica\1.5.18\hystrix-javanica-1.5.18.jar;D:\Java\repositories\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\Java\repositories\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;D:\Java\repositories\io\reactivex\rxjava-reactive-streams\1.2.1\rxjava-reactive-streams-1.2.1.jar;D:\Java\repositories\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-openfeign\2.1.1.RELEASE\spring-cloud-starter-openfeign-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-openfeign-core\2.1.1.RELEASE\spring-cloud-openfeign-core-2.1.1.RELEASE.jar;D:\Java\repositories\io\github\openfeign\form\feign-form-spring\3.5.0\feign-form-spring-3.5.0.jar;D:\Java\repositories\io\github\openfeign\form\feign-form\3.5.0\feign-form-3.5.0.jar;D:\Java\repositories\commons-fileupload\commons-fileupload\1.3.3\commons-fileupload-1.3.3.jar;D:\Java\repositories\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\Java\repositories\org\springframework\spring-web\5.1.5.RELEASE\spring-web-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-beans\5.1.5.RELEASE\spring-beans-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-commons\2.1.1.RELEASE\spring-cloud-commons-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\security\spring-security-crypto\5.1.4.RELEASE\spring-security-crypto-5.1.4.RELEASE.jar;D:\Java\repositories\io\github\openfeign\feign-core\10.1.0\feign-core-10.1.0.jar;D:\Java\repositories\io\github\openfeign\feign-slf4j\10.1.0\feign-slf4j-10.1.0.jar;D:\Java\repositories\io\github\openfeign\feign-hystrix\10.1.0\feign-hystrix-10.1.0.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-netflix-hystrix-dashboard\2.1.1.RELEASE\spring-cloud-starter-netflix-hystrix-dashboard-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-web\2.1.3.RELEASE\spring-boot-starter-web-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-json\2.1.3.RELEASE\spring-boot-starter-json-2.1.3.RELEASE.jar;D:\Java\repositories\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.8\jackson-datatype-jdk8-2.9.8.jar;D:\Java\repositories\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.8\jackson-module-parameter-names-2.9.8.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-tomcat\2.1.3.RELEASE\spring-boot-starter-tomcat-2.1.3.RELEASE.jar;D:\Java\repositories\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.16\tomcat-embed-websocket-9.0.16.jar;D:\Java\repositories\org\springframework\spring-webmvc\5.1.5.RELEASE\spring-webmvc-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-netflix-hystrix-dashboard\2.1.1.RELEASE\spring-cloud-netflix-hystrix-dashboard-2.1.1.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-freemarker\2.1.3.RELEASE\spring-boot-starter-freemarker-2.1.3.RELEASE.jar;D:\Java\repositories\org\freemarker\freemarker\2.3.28\freemarker-2.3.28.jar;D:\Java\repositories\org\webjars\jquery\2.1.1\jquery-2.1.1.jar;D:\Java\repositories\org\webjars\d3js\3.4.11\d3js-3.4.11.jar;D:\Java\repositories\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar;D:\Java\repositories\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.1.0\mybatis-spring-boot-starter-2.1.0.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-jdbc\2.1.3.RELEASE\spring-boot-starter-jdbc-2.1.3.RELEASE.jar;D:\Java\repositories\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;D:\Java\repositories\org\springframework\spring-jdbc\5.1.5.RELEASE\spring-jdbc-5.1.5.RELEASE.jar;D:\Java\repositories\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.1.0\mybatis-spring-boot-autoconfigure-2.1.0.jar;D:\Java\repositories\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\Java\repositories\org\mybatis\mybatis-spring\2.0.2\mybatis-spring-2.0.2.jar;D:\Java\repositories\com\github\pagehelper\pagehelper-spring-boot-starter\1.2.5\pagehelper-spring-boot-starter-1.2.5.jar;D:\Java\repositories\com\github\pagehelper\pagehelper-spring-boot-autoconfigure\1.2.5\pagehelper-spring-boot-autoconfigure-1.2.5.jar;D:\Java\repositories\com\github\pagehelper\pagehelper\5.1.4\pagehelper-5.1.4.jar;D:\Java\repositories\com\github\jsqlparser\jsqlparser\1.0\jsqlparser-1.0.jar;D:\Java\repositories\com\alibaba\druid-spring-boot-starter\1.1.21\druid-spring-boot-starter-1.1.21.jar;D:\Java\repositories\com\alibaba\druid\1.1.21\druid-1.1.21.jar;D:\Java\repositories\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\nethospital\ProdDev_BackEnd\common\target\classes;D:\Java\repositories\com\fasterxml\aalto-xml\0.9.11\aalto-xml-0.9.11.jar;D:\Java\repositories\org\codehaus\woodstox\stax2-api\4.0.0\stax2-api-4.0.0.jar;D:\Java\repositories\com\fasterxml\jackson\dataformat\jackson-dataformat-xml\2.9.8\jackson-dataformat-xml-2.9.8.jar;D:\Java\repositories\com\fasterxml\jackson\module\jackson-module-jaxb-annotations\2.9.8\jackson-module-jaxb-annotations-2.9.8.jar;D:\Java\repositories\com\fasterxml\woodstox\woodstox-core\5.0.3\woodstox-core-5.0.3.jar;D:\Java\repositories\com\alibaba\fastjson\1.2.69\fastjson-1.2.69.jar;D:\Java\repositories\org\apache\tomcat\embed\tomcat-embed-core\9.0.16\tomcat-embed-core-9.0.16.jar;D:\Java\repositories\org\apache\tomcat\tomcat-annotations-api\9.0.16\tomcat-annotations-api-9.0.16.jar;D:\Java\repositories\io\jsonwebtoken\jjwt\0.9.0\jjwt-0.9.0.jar;D:\Java\repositories\org\springframework\security\spring-security-core\5.1.4.RELEASE\spring-security-core-5.1.4.RELEASE.jar;D:\Java\repositories\org\springframework\spring-aop\5.1.5.RELEASE\spring-aop-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-expression\5.1.5.RELEASE\spring-expression-5.1.5.RELEASE.jar;D:\Java\repositories\redis\clients\jedis\2.9.1\jedis-2.9.1.jar;D:\Java\repositories\org\apache\commons\commons-pool2\2.6.1\commons-pool2-2.6.1.jar;D:\Java\repositories\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\nethospital\ProdDev_BackEnd\common-entity\target\classes;D:\Java\repositories\org\springframework\boot\spring-boot-starter-validation\2.1.3.RELEASE\spring-boot-starter-validation-2.1.3.RELEASE.jar;D:\Java\repositories\org\apache\tomcat\embed\tomcat-embed-el\9.0.16\tomcat-embed-el-9.0.16.jar;D:\Java\repositories\org\hibernate\validator\hibernate-validator\6.0.14.Final\hibernate-validator-6.0.14.Final.jar;D:\Java\repositories\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Java\repositories\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Java\repositories\io\springfox\springfox-swagger2\2.7.0\springfox-swagger2-2.7.0.jar;D:\Java\repositories\io\swagger\swagger-annotations\1.5.13\swagger-annotations-1.5.13.jar;D:\Java\repositories\io\swagger\swagger-models\1.5.13\swagger-models-1.5.13.jar;D:\Java\repositories\io\springfox\springfox-spi\2.7.0\springfox-spi-2.7.0.jar;D:\Java\repositories\io\springfox\springfox-core\2.7.0\springfox-core-2.7.0.jar;D:\Java\repositories\io\springfox\springfox-schema\2.7.0\springfox-schema-2.7.0.jar;D:\Java\repositories\io\springfox\springfox-swagger-common\2.7.0\springfox-swagger-common-2.7.0.jar;D:\Java\repositories\io\springfox\springfox-spring-web\2.7.0\springfox-spring-web-2.7.0.jar;D:\Java\repositories\org\reflections\reflections\0.9.11\reflections-0.9.11.jar;D:\Java\repositories\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;D:\Java\repositories\com\google\guava\guava\18.0\guava-18.0.jar;D:\Java\repositories\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\Java\repositories\org\springframework\plugin\spring-plugin-core\1.2.0.RELEASE\spring-plugin-core-1.2.0.RELEASE.jar;D:\Java\repositories\org\springframework\plugin\spring-plugin-metadata\1.2.0.RELEASE\spring-plugin-metadata-1.2.0.RELEASE.jar;D:\Java\repositories\org\mapstruct\mapstruct\1.1.0.Final\mapstruct-1.1.0.Final.jar;D:\Java\repositories\io\springfox\springfox-swagger-ui\2.7.0\springfox-swagger-ui-2.7.0.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-data-redis\2.1.3.RELEASE\spring-boot-starter-data-redis-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\data\spring-data-redis\2.1.5.RELEASE\spring-data-redis-2.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\data\spring-data-keyvalue\2.1.5.RELEASE\spring-data-keyvalue-2.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\data\spring-data-commons\2.1.5.RELEASE\spring-data-commons-2.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-tx\5.1.5.RELEASE\spring-tx-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-oxm\5.1.5.RELEASE\spring-oxm-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\spring-context-support\5.1.5.RELEASE\spring-context-support-5.1.5.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-starter-security\2.1.2.RELEASE\spring-cloud-starter-security-2.1.2.RELEASE.jar;D:\Java\repositories\org\springframework\cloud\spring-cloud-security\2.1.2.RELEASE\spring-cloud-security-2.1.2.RELEASE.jar;D:\Java\repositories\org\springframework\boot\spring-boot-starter-security\2.1.3.RELEASE\spring-boot-starter-security-2.1.3.RELEASE.jar;D:\Java\repositories\org\springframework\security\spring-security-config\5.1.4.RELEASE\spring-security-config-5.1.4.RELEASE.jar;D:\Java\repositories\org\springframework\security\spring-security-web\5.1.4.RELEASE\spring-security-web-5.1.4.RELEASE.jar;D:\Java\repositories\org\apache\poi\poi\3.16\poi-3.16.jar;D:\Java\repositories\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\Java\repositories\org\apache\commons\commons-collections4\4.1\commons-collections4-4.1.jar;D:\Java\repositories\org\apache\poi\poi-ooxml\3.16\poi-ooxml-3.16.jar;D:\Java\repositories\org\apache\poi\poi-ooxml-schemas\3.16\poi-ooxml-schemas-3.16.jar;D:\Java\repositories\org\apache\xmlbeans\xmlbeans\2.6.0\xmlbeans-2.6.0.jar;D:\Java\repositories\com\github\virtuald\curvesapi\1.04\curvesapi-1.04.jar;D:\Java\repositories\net\bytebuddy\byte-buddy\1.9.10\byte-buddy-1.9.10.jar com.hospital.hospital.service.impl.ImportServiceImpl
Exception in thread "main" java.lang.NullPointerException
at com.hospital.hospital.service.impl.ImportServiceImpl.main(ImportServiceImpl.java:160)
Process finished with exit code 1
怎么才能优雅的解决这个问题呢!
答案当然是有的,jdk1.8 的TOptional 为我们提供了优雅的解决方案,那么我们先看看源码
/**
* Returns an {@code Optional} with the specified present non-null value.
*
* @param <T> the class of the value
* @param value the value to be present, which must be non-null
* @return an {@code Optional} with the value present
* @throws NullPointerException if value is null
*/
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
/**
* Constructs an instance with the value present.
*
* @param value the non-null value to be present
* @throws NullPointerException if value is null
*/
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
顺着方法一步步走查,发现,如果T value是null,那么使用Optional.of(T value)就会手动抛出NullPointerException()。
所以,使用该方法格外注意,不能有null。
使用Optional.ofNullable,看源码
/**
* Returns an {@code Optional} describing the specified value, if non-null,
* otherwise returns an empty {@code Optional}.
*
* @param <T> the class of the value
* @param value the possibly-null value to describe
* @return an {@code Optional} with a present value if the specified value
* is non-null, otherwise an empty {@code Optional}
*/
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
/**
* Returns an empty {@code Optional} instance. No value is present for this
* Optional.
*
* @apiNote Though it may be tempting to do so, avoid testing if an object
* is empty by comparing with {@code ==} against instances returned by
* {@code Option.empty()}. There is no guarantee that it is a singleton.
* Instead, use {@link #isPresent()}.
*
* @param <T> Type of the non-existent value
* @return an empty {@code Optional}
*/
public static<T> Optional<T> empty() {
@SuppressWarnings("unchecked")
Optional<T> t = (Optional<T>) EMPTY;
return t;
}
/**
* Common instance for {@code empty()}.
*/
private static final Optional<?> EMPTY = new Optional<>();
/**
* Constructs an empty instance.
*
* @implNote Generally only one empty instance, {@link Optional#EMPTY},
* should exist per VM.
*/
private Optional() {
this.value = null;
}
/**
* Return the value if present, otherwise return {@code other}.
*
* @param other the value to be returned if there is no value present, may
* be null
* @return the value, if present, otherwise {@code other}
*/
public T orElse(T other) {
return value != null ? value : other;
}
这个方法可以,如果value为null,那么就会手动创建一个new Optional();但是this.value = null。所以这个时候就跳出了手动抛空指针那个代码块。这个时候,我们就可以使用orElse(T other)。看源码,如果value == null,那么将取other的值
除了上面讲的ofNullable(),orElse(), 还有orElseGet(), isPresent(),map(),filter(),flatMap(),orElseThrow() 等等使用方法,有时间的可以深入研究一下。。。