java if (name!=null name!=),java中的NullPointerException异常
java中的NullPointerException異常
關注:176??答案:3??mip版
解決時間 2021-01-27 20:59
提問者侢遇噹姩揂
2021-01-27 02:10
Login.jsp提供登錄表單。到LoginCheck.jsp發生空指針異常錯誤。
LoginCheck.jsp:
String userName = request.getParameter("userName");
String passward = request.getParameter("passward");
if(userName.length()>0&&passward.length()>0)
{
session.setAttribute("status","Login");
response.sendRedirect("Main.jsp");}
else
response.sendRedirect("Login.jsp");%>
最佳答案
二級知識專家無字情書
2021-01-27 02:52
String userName = request.getParameter("userName"); //這里取值的時候 有可能會取到 null
String passward = request.getParameter("passward"); //這里取值的時候 有可能會取到 null
if(userName.length()>0&&passward.length()>0) //如果這里 userName == null || passward ==nul,你引用字符串的 length() 方法的時候,就會 出現異常了,因為 字符串對象不存在,卻引用了他的方法,最好 加一下判斷 不為空的時候 判斷字符串長度
{
1、if(userName!=null&&passward!=null&&userName.length()>0&&passward.length()>0)
2、或者接受參數的時候就做調整
String userName = request.getParameter("userName") == null ? "":request.getParameter("userName") ;
String passward = request.getParameter("passward")== null ?"":request.getParameter("passward");
全部回答
1樓久別無恙
2021-01-27 05:01
最好還是發異常的控制臺輸出的信息吧,那個描述非常清晰,看一眼就知道是哪個類、哪個方法、哪一行出的問題。
如果沒有輸出定位信息,用try-catch捕捉一下。
2樓薯片軟妹
2021-01-27 03:44
因為目錄有可能是空目錄,即目錄下沒有文件,所以path.list() 返回為null,所以需要在for循環前判斷一下,加一句if(files!=null)就行了。完整的程序如下:
import java.io.file;
import java.io.ioexception;
public class traverse {
public static void tra(file path){
if(path == null){
return;
}
if(path.isdirectory()){
string[] files = path.list();
if(files!=null) //這里加一句
for(int i = 0; i < files.length;i++){
tra(new file(path,files[i]));
}
}else if(path.getabsolutepath().endswith(".mp3")){
system.out.println(path);
}
}
public static void main(string[] args) {
try{
file file = new file("d:\\");
tra(file);
}catch(exception e){
e.printstacktrace();
}
}
}
我要舉報
如以上問答內容為低俗/色情/暴力/不良/侵權的信息,可以點下面鏈接進行舉報,我們會做出相應處理,感謝你的支持!
→點此我要舉報以上信息!←
推薦資訊
大家都在看
總結
以上是生活随笔為你收集整理的java if (name!=null name!=),java中的NullPointerException异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: debian 删除mysql数据库_De
- 下一篇: mapbox矢量切片标准_Cesium