null .
, " " ( ). NPE , , , .
, null - , " , ".
, NPE, null-safety. - , null , . , , - null .
, , . Java EE - Java Servlet Specification, HttpServletRequest getCookies()
getCookies
Cookie[] getCookies()
Returns an array containing all of the Cookie
objects the client sent with this request. This method returns null
if no cookies were sent.
Returns:
an array of all the
Cookies
included with this request, ornull
if the request has no cookies
:
This method returns
null
if no cookies were sent.
, , null. :
, , -, , .
null
, , . ? , ?
null vs empty array
null
, , :
API, null-check
- , getCookies() null .
. ,
null
( )
, , . null , , ( ).
, .
-, , . , , , .
-, (, - )
, null
for (Cookie cookie : httpServletRequest.getCookies()) {
// NPE! // …
}
int cookiesSize = httpServletRequest.getCookies().length // NPE!
null-check:
if (httpServletRequest.getCookies() != null)
for (Cookie cookie : httpServletRequest.getCookies()) {
// …
}
Cookie[] cookies = httpServletRequest.getCookies();
int cookiesSize = cookies == null ? 0 : cookies.length
, , NPE . , .
API, . Jetty
, , .
:
return cookies == null?null:cookies.getCookies();
:
if (cookies == null || cookies.getCookies().length == 0)
return null;
return _cookies.getCookies();
, .
, , . null
, , null
. , .
!
The GNU Classpath Extensions project, aka classpathx builds free versions of Oracle's Java extension libraries, the packages in the
javax
namespace. It is a companion project of the GNU Classpath project.
Cookie[] getCookies()
Gets all the Cookies present in the request.
Returns:
an array containing all the Cookies or an empty array if there are no cookies
Since:
2.0
. null
. , Sonar, SEI CERT Oracle Coding Standart for Java
. , , , .
.
- , null . , , , , . , .
, , , . null , .
- ( ):
- ,
. - , , .
, , .
Speaking at a software conference in 2009, Tony Hoare apologized for inventing the null reference:[25]
I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years