¡¡¡¡1¡¢¿ÕÖ¸Õë´íÎó java.lang.NullPointerException
??ʹÓûù±¾µÄJAVAÊý¾ÝÀàÐÍ£¬±äÁ¿µÄֵҪôÒѾÊÇĬÈÏÖµ£¬Èç¹ûûÓжÔÆäÕý³£¸³Öµ£¬³ÌÐò±ã²»ÄÜͨ¹ý±àÒ룬Òò´ËʹÓûù±¾µÄJAVAÊý¾ÝÀàÐÍ(double,float,boolean,char,int,long)Ò»°ã²»»áÒýÆð¿ÕÖ¸ÕëÒì³£¡£Óɴ˿ɼû£¬¿ÕÖ¸ÕëÒì³£Ö÷Òª¸úÓë¶ÔÏóµÄ²Ù×÷Ïà¹Ø¡£
??ÏÂÃæÏÈÁгöÁË¿ÉÄÜ·¢Éú¿ÕÖ¸ÕëÒì³£µÄ¼¸ÖÖÇé¿ö¼°ÏàÓ¦½â¾ö·½°¸£º
??²»¹Ü¶ÔÏóÊÇ·ñΪ¿Õ¾ÍÖ±½Ó¿ªÊ¼Ê¹Óá£
??(JSP)´úÂë¶Î1£º
??out.println(request.getParameter("username"));
??ÃèÊö£º
??´úÂë¶Î1µÄ¹¦ÄÜÊ®·Ö¼òµ¥£¬¾ÍÊÇÊä³öÓû§ÊäÈëµÄ±íÓò"username"µÄÖµ¡£
??˵Ã÷£º
??¿´ÉÏÈ¥£¬ÉÏÃæµÄÓï¾äÕÒ²»³öʲôÓï·¨´íÎ󣬶øÇÒÔÚ´ó¶àÊýÇé¿öÏÂÒ²Óö²»µ½Ê²Ã´ÎÊÌâ¡£µ«ÊÇ£¬Èç¹ûij¸öÓû§ÔÚÊäÈëÊý¾Ýʱ²¢Ã»ÓÐÌṩ±íµ¥Óò"username"µÄÖµ£¬»òͨ¹ýijÖÖ;¾¶Èƹý±íµ¥Ö±½ÓÊäÈëʱ£¬´Ëʱrequest.getParameter("username")µÄֵΪ¿Õ£¨²»ÊÇ¿Õ×Ö·û´®£¬ÊÇ¿Õ¶ÔÏónull¡££©£¬out¶ÔÏóµÄprintln·½·¨ÊÇÎÞ·¨Ö±½Ó¶Ô¿Õ¶ÔÏó²Ù×÷£¬Òò´Ë´úÂë¶Î1ËùÔÚµÄJSPÒ³Ã潫»áÅ׳ö"java.lang.NullPointerException"Òì³£¡£
??¼´Ê¹¶ÔÏó¿ÉÄÜΪ¿Õʱ£¬Ò²µ÷ÓÃjava.lang.Object»òObject¶ÔÏó±¾ÉíµÄһЩ·½·¨ÈçtoString(), equals(Object obj)µÈ²Ù×÷¡£
??(JSP)´úÂë¶Î2£º
??String userName = request.getParameter("username");
??If (userName.equals("root"))
??{....}
??ÃèÊö£º
??´úÂë¶Î2µÄ¹¦ÄÜÊǼì²âÓû§ÌṩµÄÓû§Ãû£¬Èç¹ûÊÇÓû§Ãû³ÆΪ"root"µÄÓû§Ê±£¬¾ÍÖ´ÐÐһЩÌرðµÄ²Ù×÷¡£
??˵Ã÷£º
??ÔÚ´úÂë¶Î2ÖУ¬Èç¹ûÓÐÓû§Ã»ÓÐÌṩ±íµ¥Óò"username"µÄֵʱ£¬×Ö·û´®¶ÔÏóuserNameΪnullÖµ£¬²»Äܹ»½«Ò»¸önullµÄ¶ÔÏóÓëÁíÒ»¸ö¶ÔÏóÖ±½Ó±È½Ï£¬Í¬Ñù£¬´úÂë¶Î2ËùÔÚµÄJSPÒ³Ãæ¾Í»áÅ׳ö(java.lang.NullPointerException)¿ÕÖ¸Õë´íÎó¡£
??(JSP)´úÂë¶Î3£º
??String userName = session.getAttribute("session.username").toString();
??ÃèÊö£º
??´úÂë¶Î3µÄ¹¦ÄÜÊǽ«sessionÖÐsession.usernameµÄֵȡ³ö£¬²¢½«¸ÃÖµ¸³¸ø×Ö·û´®¶ÔÏó userName¡£
??˵Ã÷£º
??ÔÚÒ»°ãÇé¿öÏ£¬Èç¹ûÔÚÓû§ÒѾ½øÐÐij¸ö»á»°£¬Ôò²»»á³öÏÖʲôÎÊÌ⣻µ«ÊÇ£¬Èç¹û´ËʱӦÓ÷þÎñÆ÷ÖØÐÂÆô¶¯£¬¶øÓû§»¹Ã»ÓÐÖØеǼ£¬£¨Ò²¿ÉÄÜÊÇÓû§¹Ø±Õä¯ÀÀÆ÷£¬µ«ÊÇÈÔ´ò¿ªÔÀ´µÄÒ³Ãæ¡££©ÄÇô£¬´Ëʱ¸ÃsessionµÄÖµ¾Í»áʧЧ£¬Í¬Ê±µ¼ÖÂsessionÖеÄsession.usernameµÄֵΪ¿Õ¡£¶ÔÒ»¸öΪnullµÄ¶ÔÏóµÄÖ±½ÓÖ´ÐÐtoString()²Ù×÷£¬¾Í»áµ¼ÖÂϵͳÅ׳ö(java.lang.NullPointerException)¿ÕÖ¸ÕëÒì³£¡£
??½â¾ö·½°¸£º
??ΪÁËÈ·±£½øÐвÙ×÷»òÒýÓõĶÔÏó·Ç¿Õ£¬¼ÙÈôÎÒÃÇÒª¶Ôij¶ÔÏó½øÐвÙ×÷»òÒýÓã¬ÎÒÃÇÊ×ÏÈÈ¥¼ì²é¸Ã¶ÔÏóÊÇ·ñÒѾʵÀý»¯ÇÒ²»Îª¿Õ£»²¢ÇÒÔÚϵͳÖмÓÈëÕë¶Ô¶ÔÏóΪ¿ÕʱÇé¿öµÄ´¦Àí¡£
??È磺²ÉÓÃString¶ÔÏó±£´æÓû§Ìá½»µÄ½á¹û£»ÔÚÈç¹ûÉæ¼°¶ÔÏóµÄ²Ù×÷ʱ£¬Ïȼì²âÆäÊÇ·ñΪ¿Õºó£¬¼ì²éµ½¶ÔÏóΪ¿Õºó£¬¿ÉÔÙÑ¡Ôñ½øÐÐÒÔÏÂÈÎÒ»ÖÖ´¦Àí·½Ê½£º
??´¦Àí·½Ê½ 1) ¼ì²éµ½¶ÔÏóΪ¿Õʱ£¬ÉèÖöÔÏóֵΪ¿Õ×Ö·û´®»òÒ»¸öĬÈÏÖµ£»
??´¦Àí·½Ê½ 2) ¼ì²âµ½¶ÔÏóΪ¿Õʱ£¬¸ù±¾²»Ö´ÐÐij²Ù×÷£¬Ö±½ÓÌøתµ½ÆäËû´¦ÀíÖС£
??´¦Àí·½Ê½ 3) ¼ì²éµ½¶ÔÏóΪ¿Õʱ£¬ÌáʾÓû§²Ù×÷ÓдíÎó¡£
??½«´úÂë¶Î2°´ÒÔÉÏ·½Ê½½øÐиÄд£¬µÃµ½£º
??·½Ê½1£º
??String userName = request.getParameter("username");
??// ¸Ã±äÁ¿ÖµÎª¿Õʱ£¬×ª»¯ÎªÄ¬ÈÏ¿Õ×Ö·û´®
??If (userName == null)
??userName = "";
??If (userName.equals("root"))
??{..........}
??·½Ê½2£º
??String userName = request.getParameter("username");
??// ¸Ã±äÁ¿ÖµÎª¿Õʱ£¬×ª»¯ÎªÄ¬ÈÏ¿Õ×Ö·û´®£¬²»Ö´ÐÐÓйزÙ×÷¡£
??If (usreName != null)
??{
???If (userName.equals("root"))
???{..........}
??}
??·½Ê½3£º
??String userName = request.getParameter("username");
??// ¸Ã±äÁ¿ÖµÎª¿Õʱ£¬×ª»¯ÎªÄ¬ÈÏ¿Õ×Ö·û´®£¬²»Ö´ÐÐÓйزÙ×÷¡£
??If (usreName == null)
??{
??// ÌáʾÓû§ÊäÈëÐÅϢΪ¿Õ
??}
??ʵ¼ÊÖУ¬ÉÏÃæÌṩµ½ÈýÖÖ´¦Àí·½Ê½Ò²Í¬ÑùÊÊÓÃÓÚÆäËûÒì³£µÄ´¦Àí£º
??Òì³£´¦Àí·½Ê½ 1) ¼ì²éµ½Òì³£³öÏÖ£¬ÉèÖöÔÏóֵΪ¿Õ×Ö·û´®»òÒ»¸öĬÈÏÖµ¡£
??Òì³£´¦Àí·½Ê½ 2) ¼ì²âµ½Òì³£³öÏÖ£¬¸ù±¾²»Ö´ÐÐij²Ù×÷£¬Ö±½ÓÌøתµ½ÆäËû´¦ÀíÖС£
??Òì³£´¦Àí·½Ê½ 3) ¼ì²éµ½Òì³£³öÏÖ£¬ÌáʾÓû§²Ù×÷ÓдíÎó¡£
??2¡¢¸ñʽ»¯Êý×Ö´íÎó java.lang.NumberFormatException·ÖÎö
??(JSP)´úÂë¶Î3£º
??String s_memberid = request.getParameter("memberid");
??int i_memberid = Integer.parseInt(s_memberid);
??ÃèÊö£º
??ÒÔÉÏ´úÂë¶ÎµÄ×÷ÓÃÊǽ«Óû§Ìá½»µÄ±íµ¥ÓòmemberidµÄֵת»¯ÎªÕûÊý¡£
??˵Ã÷£º
??Èç¹ûÓû§ÊäÈëÕýÈ·µÄÊý×ÖÈ磺1082£¬²»»áÓÐʲôÎÊÌ⡣Ȼ¶ø£¬Èç¹ûÓû§ÊäÈëT1082ʱ£¬ÓÉÓÚT1082²»ÊǺϷ¨µÄÊý×Ö¸ñʽ£¬JAVAÎÞ·¨½«Æäת»¯ÎªºÏÊʵÄÊý×Ö£¬µ¼ÖÂÅ׳öjava.lang.NumberFormatExceptionÊý×Ö¸ñʽ»¯Òì³£¡£
??½â¾ö·½°¸£º
??ÔÚÈκÎÓõ½×Ö·û´®×ª»¯ÎªÊý×Öʱ£¬²¶×½Òì³££¬¶ÔÒì³£Çé¿ö½øÐд¦Àí°´Òì³£´¦Àí·½Ê½1£º¼ì²éµ½Òì³£·¢Éú£¬¼´¸³¸øij±äÁ¿Ò»¸öĬÈÏÖµ£»£¨¿ÉÄÜÔÚijЩÇé¿öϵ¼ÖÂÒ»ÆäËû³ÌÐò´íÎó[±È·½ËµÆäËûÄ£¿éÖв¢Î´´¦ÀíÄúËù¸³ÓèµÄĬÈÏÖµÇé¿ö£¬¿ÉÄܵ¼ÖÂһЩÒì³£»ò´íÎó³öÏÖ¡£]£©°´Òì³£´¦Àí·½Ê½3£º¼ì²éµ½Òì³£·¢Éú£¬ÌáʾÓû§Ê¹ÓÃÕýÈ·µÄÊý×Ö¸ñʽÊäÈë¡££¨ÊµÏÖÉÔ΢Âé·³Ò»µã£¬µ«Êǽ«´íÎó×èµ²ÔÚÄúµÄÄ£¿éÇ°[¼´ÄúÌṩ¸øÆäËûÄ£¿éµÄÖµ¾ùÊÇ°²È«µÄ]¡££©°´ÕâÖÖ·½·¨¶Ô³ÌÐò½øÐиÄд£¬ÔÚ±à³ÌʱÉÔ΢Âé·³Ò»µã£¬µ«ÕâµÄÈ·»áÄúµÄÄ£¿é¸ü¼Ó½¡×³¡£½«´úÂë¶Î3°´ÒÔÉÏÒªÇó½øÐиÄд£¬µÃµ½£º
??String s_memberid = request.getParameter("memberid");
??int i_memberid;
??try
??{
???i_memberid = Integer.parseInt(s_memberid);
???...
??}
??catch(NumberFormatException nfe)
??{
???//·½Ê½1£º£¨¼òµ¥£¬Ö±½Ó¸ø¸Ã±àºÅΪһ¸öĬÈÏÖµ0£»£©
???i_memberid = 0;
???//·½Ê½2£º£¨ºÜ¼òªµÄ×ö·¨£¬½¨ÒéʹÓøüÓѺõÄÌáʾ·½Ê½£©
???out.println("");
??}
??3¡¢×Ö·û´®Ô½½ç´íÎó java.lang.StringIndexOutOfBoundsExceptionµÈ×Ö·û´®ÀàÏà¹Ø´íÎó
??´úÂë¶Î4£º
??String s_all_power = "1010011";
??String s_access_power = s_all_power.substring(3,4);
??ÃèÊö£º
??ÒÔÉÏ´úÂë¶Î¹¦ÄܵŦÄÜÊÇ»ñÈ¡×Ö·û´®s_all_powerÖеĵÚ4¸ö×Ö·û¡£
??˵Ã÷£º
??Ò»°ãÇé¿öÏ£¬³ÌÐò²»»áÓÐÎÊÌ⣬Èç¹ûÓÉÓÚijÖÖÔÒò£¬s_all_power³¤¶È±ä¶Ì£¬³ÌÐò¾Í»áÅ׳ö×Ö·û´®´íÎó¡£
??½â¾ö·½°¸£º
??¶Ô×Ö·û´®½øÐнØÈ¡(substring, charAt)¡¢×ª»»Îª×Ö½ÚÊý×é(getBytes)£¬×Ö·ûÊý×éת»»Îª×Ö·û´®£¨valueOf£©²Ù×÷ʱ£¬ÏȶԲÙ×÷×Ö·û´®¶ÔÏóµÄ´æÔÚÐÔ£¨ÊÇ·ñΪ¿Õ£©¼°³¤¶È½øÐмì²éºó£¬ÔÙ½øÐвÙ×÷¡£
??¸ÄдµÃµ½£º
??String s_all_power = "1010011";
??if (s_all_power.length>4)
???String s_access_power = s_all_power.substring(3,4);
??4¡¢ÀඨÒåδÕÒµ½´íÎó java.lang.NoClassDefFoundError
??ÔÒò£º
??ÓÉÓڸóÌÐòµ÷Óõ½µÄJAVAÀàÎļþûÓÐÕýÈ·ÉÏ´«£»
??½â¾ö·½°¸£º
??½«JAVAÀàÎļþÖØÐÂÉÏ´«¡£
??JAVAÀàÎļþÒѾÉÏ´«£¬µ«Ó¦Ó÷þÎñÆ÷²¢Î´¼ì²âµ½£¬½¨Ò齫JSPÒ³ÃæÖØиüС£
??½â¾ö·½°¸£º
??½«JSPÒ³Ãæ¸üв¢ÉÏ´«£»»ò½«Ó¦ÓóÌÐò·þÎñÆ÷½øÐÐÖØÐÂÆô¶¯¡£
??5¡¢JAVA´íÎó java.lang.Error
??ÔÒò:
??1.¶ÔϵͳËù·ÃÎÊÍⲿ×ÊÔ´£¬Î´Ö´ÐйرղÙ×÷£¬µ¼ÖÂÍⲿ×ÊÔ´´óÁ¿ÀË·Ñ£¬×îÖÕ¿ÉÄܵ¼ÖÂϵͳÎÞ·¨Õý³£ÔËÐУ»
??2.¶ÔϵͳËù·ÃÎʵÄÍⲿ×ÊÔ´¹Ø±Õ´ÎÊýÌ«¶à£¬ÍⲿϵͳÎÞ·¨Õý³£´¦Àí£»
??3.ϵͳ·ÃÎʵÄÍⲿ×ÊÔ´³öÏÖÒì³£Çé¿ö¡£
??½â¾ö·½°¸£º
??1.·ÃÎÊÍⲿ×ÊÔ´Ç°£¬Ê×Ïȼì²é¸Ã×ÊÔ´(ÈçÊý¾Ý¿â)ÊÇ·ñ¿ÉÕý³£Á¬½Ó»ò²Ù×÷¡£
??2.·ÃÎÊÍⲿ×ÊԴʱ£¬Èç¹û½øÐÐÁËÁ¬½Ó£¬Ò»¶¨½øÐйرղÙ×÷£¬²¢½ö½øÐÐÒ»´Î¹Ø±Õ²Ù×÷¡£
??3.¾¡Á¿ÔÚͬһ²Ù×÷Öй²ÏíÍⲿ×ÊÔ´£¬ÒÔ¼õÉٸòÙ×÷¶Ô×ÊÔ´µÄÏû·Ñ£¬Ìá¸ß³ÌÐòµÄÖ´ÐÐЧÂÊ¡£
Myiozzdoc ÓÚ 2006-08-09 10:06:29·¢±í:
ллָ½Ì