璇诲彇鏂囦欢,鍑虹幇浜唈ava.lang.NullPointerException???

时间:2023-01-19 21:36:12
浠ヤ笅璇诲彇file鏂囨。鏃舵姤java.lang.NullPointerException


<%@ page contentType="text/html; charset=gb2312" import="java.io.*"%> 

<%
try
{
//浣跨敤ServletContext瑁呭叆鏂囦欢璧勬簮
InputStream in=getServletContext().getResourceAsStream("/file.txt");
String file="";
int temp=0;
while((temp=in.read())!=-1)
{
  file+=(char)temp;
 }
 //鍏抽棴杈撳叆娴併€?
 in.close();
 out.println(new String(file.getBytes("iso-8859-1")));  
 out.flush();
}
catch(Exception e)
{
  out.println(e);
  e.printStackTrace();
}
%>
   



13 涓В鍐虫柟妗?/h2>

#1


InputStream   in=getServletContext().getResourceAsStream("/file.txt"); 
in=null?

#2


in搴旇涓嶄細绛変簬null鍟?绋嬪簭鍒板簳閿欏湪鍝噷浜嗗晩?

#3


涓轰粈涔堣鍦↗SP閲岄潰鍘诲啓杩欑浠g爜锛?

#4


鎴戞槸鍦ㄥ涔犵殑杩囩▼涓?杩欐槸涔︿笂鐨勪緥瀛?鍙槸涓嶇煡閬撲负浠€涔堜細鍑虹幇杩欐牱鐨勯敊璇?甯垜璋冭瘯涓嬪惂~

#5


file.txt鏀惧湪鏍圭洰褰曚笅锛?
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂

#6


濡傛灉绋嬪簭鏄繖涓€娈电殑璇?!娌℃湁姣?
in == null鐨勫彲鑳芥€уぇ鐨勪簡

鍏堝垽鏂璱n鏄惁==null

if(in!=null)
while((temp=in.read())!=-1) 

    file+=(char)temp; 
  } 
  //鍏抽棴杈撳叆娴併€?nbsp;
  in.close(); 
  out.println(new   String(file.getBytes("iso-8859-1")));   
  out.flush(); 

catch(Exception   e) 

    out.println(e); 
    e.printStackTrace(); 

#7


寰堟槑鏄炬槸娌℃壘鍒版枃浠?鐩綍涓嶅,瀹炲湪涓嶈鐢ㄧ粷瀵硅矾寰?

#8


濂借薄鏄痠n涓虹┖鐨勫晩,闅鹃亾鏄?"/file.txt")鐨勯棶棰樺悧?file.txt涓庢垜鐨勬枃浠跺湪鍚屼竴涓洰褰曚笅鐨?

#9


/file.txt琛ㄧず鍦ㄦ牴鐩綍涓嬶紝浣犳槸鏀惧湪鏍圭洰褰曚笅鍚楋紵

#10


鍦ㄥ悓涓€涓洰褰曚笅灏变笉鐢ㄥ姞 /

#11


鎴戠幇鍦ㄥ彂鐜伴棶棰樹簡,瑕佸皢file鏂囨。鏀惧埌C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\ 鐨勪笅闈㈡墠鑳借В鍐抽棶棰?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?

#12


tomcat浠栭粯璁ょ殑鐩綍灏辨槸myapp\ 

#13


浣犳槑鐧戒粈涔堟槸鏍圭洰褰曞悧锛?/"灏变唬琛ㄦ牴鐩綍锛屼篃灏辨槸浣犺鐨刴yapp杩欎釜鐩綍锛岄偅涔堜綘鍐欑殑"/file.txt"灏变唬琛ㄦ槸myapp/file.txt!
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
      getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡 

#1


InputStream   in=getServletContext().getResourceAsStream("/file.txt"); 
in=null?

#2


in搴旇涓嶄細绛変簬null鍟?绋嬪簭鍒板簳閿欏湪鍝噷浜嗗晩?

#3


涓轰粈涔堣鍦↗SP閲岄潰鍘诲啓杩欑浠g爜锛?

#4


鎴戞槸鍦ㄥ涔犵殑杩囩▼涓?杩欐槸涔︿笂鐨勪緥瀛?鍙槸涓嶇煡閬撲负浠€涔堜細鍑虹幇杩欐牱鐨勯敊璇?甯垜璋冭瘯涓嬪惂~

#5


file.txt鏀惧湪鏍圭洰褰曚笅锛?
鎴戞棤娉曞府浣犺皟璇曪紝鍥犱负鎴戞病鏈変綘鐨勭幆澧冦€?
涓€鑸琂SP鏂囦欢閮戒細缂栬瘧鎴愪竴涓猄ervlet鏂囦欢锛屼綘鍙互鎵惧埌涓庝綘鐨凧SP鏂囦欢瀵瑰簲鐨?java鏂囦欢锛岀劧鍚庢槸鍦ㄤ笉琛屽氨璁句釜
鏂偣Debug涓€涓嬶紝鍏堟壘鍒板摢涓€间负NULL浜嗗惂

#6


濡傛灉绋嬪簭鏄繖涓€娈电殑璇?!娌℃湁姣?
in == null鐨勫彲鑳芥€уぇ鐨勪簡

鍏堝垽鏂璱n鏄惁==null

if(in!=null)
while((temp=in.read())!=-1) 

    file+=(char)temp; 
  } 
  //鍏抽棴杈撳叆娴併€?nbsp;
  in.close(); 
  out.println(new   String(file.getBytes("iso-8859-1")));   
  out.flush(); 

catch(Exception   e) 

    out.println(e); 
    e.printStackTrace(); 

#7


寰堟槑鏄炬槸娌℃壘鍒版枃浠?鐩綍涓嶅,瀹炲湪涓嶈鐢ㄧ粷瀵硅矾寰?

#8


濂借薄鏄痠n涓虹┖鐨勫晩,闅鹃亾鏄?"/file.txt")鐨勯棶棰樺悧?file.txt涓庢垜鐨勬枃浠跺湪鍚屼竴涓洰褰曚笅鐨?

#9


/file.txt琛ㄧず鍦ㄦ牴鐩綍涓嬶紝浣犳槸鏀惧湪鏍圭洰褰曚笅鍚楋紵

#10


鍦ㄥ悓涓€涓洰褰曚笅灏变笉鐢ㄥ姞 /

#11


鎴戠幇鍦ㄥ彂鐜伴棶棰樹簡,瑕佸皢file鏂囨。鏀惧埌C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\ 鐨勪笅闈㈡墠鑳借В鍐抽棶棰?
浣嗘槸鎴戠殑jsp鏂囦欢鏄湪C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myapp\JSP2.0\ch10閲岄潰鐨?
鐜板湪鎴戞兂鐭ラ亾,
涓轰粈涔堣鏀惧湪myapp涓嬮潰鍛?闅鹃亾鏀惧湪myapp\JSP2.0\ch10涓嬮潰,鍜宩sp鏂囦欢鍚屼竴涓洰褰?涓嶈鍚?
鎴戜篃娌℃湁鏀逛粈涔堥厤缃枃浠?

#12


tomcat浠栭粯璁ょ殑鐩綍灏辨槸myapp\ 

#13


浣犳槑鐧戒粈涔堟槸鏍圭洰褰曞悧锛?/"灏变唬琛ㄦ牴鐩綍锛屼篃灏辨槸浣犺鐨刴yapp杩欎釜鐩綍锛岄偅涔堜綘鍐欑殑"/file.txt"灏变唬琛ㄦ槸myapp/file.txt!
鏀惧湪鍚屼竴涓洰褰曚笅褰撶劧鍙互锛屼絾鏄綘鍘诲彇鐨勬椂鍊欑‘鏄敤鐨勮繖绉嶆柟寮忥細
      getServletContext().getResourceAsStream("/file.txt");
鍏朵腑getServletContext()杩欎釜鍙栫殑鏄牴鐩綍锛岄偅涔堝綋鐒跺畠璁や负浣犵殑璺緞鏄?myapp/file.txt浜?
浣犺杩欐牱鍐?"/JSP2.0/ch10/file.txt")灏卞彲浠ヤ簡