當寫程式時常常會用到以下的方法,來存取一個傳來的變量
1 | String username = request.getParameter("username"); |
同時我也許會對這些變量進行一些相關的檢查,如
1 | if (username.isEmpty()) { |
但是這常常這都會出現一些 null pointer 的錯誤.那就必須要使用以下方法
1 | if (username != null && username.isEmpty()) { } |
其實可以通過這種方法來解決?
1 | if ("".equals(username)) { } |