厚着脸皮来问个简单的问题:java.lang.NullPointerException是如何产生的?应该如何解决?谢谢!

Smilings 2003-04-30 09:01:12
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
* <p>Title: BStock</p>
* <p>Description: The programe of BStock.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: GC</p>
* @author s
* @version 1.0
*/

public class Servlet2 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
String str;

//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
str=request.getParameter("test");
if(str.equals(null)) out.println("Sorry!");
else {
out.println(str);
}
out.println("<font color=\"green\">");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</font>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
doGet(request,response);
}
//Clean up resources
public void destroy() {
}
}

比如在这个简单的例子,因为request.getParameter(),如果传过来的参数为空,那么就会出现空指针,这样起容错性就很不好了;如果要解决这个简单的问题,该做怎样的处理?
谢谢!
...全文
52 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Smilings 2003-05-01
  • 打赏
  • 举报
回复
谢谢!
问题解决,揭贴啦!~~~~~~~~~

binny 2003-05-01
  • 打赏
  • 举报
回复
if (index_stock != null && index_stock.equals(ConstantConfig.MSG_STOCK_WANT))
^^
sgdb 2003-05-01
  • 打赏
  • 举报
回复
if ((!index_stock.equals(""))&index_stock.equals(ConstantConfig.MSG_STOCK_WANT))
wellsoon 2003-05-01
  • 打赏
  • 举报
回复
是不是这句的问题???
改成:
if ((index_stock!=null)&(index_stock.equals(ConstantConfig.MSG_STOCK_WANT)))

可以么???
rocandroc 2003-04-30
  • 打赏
  • 举报
回复
GZ
Smilings 2003-04-30
  • 打赏
  • 举报
回复
其实这个是我自己简化学习的一个例子,在我发现问题的程序中有下面的一段:
在这里,我用的是!=null,但在运行时,也是抛出了空指针异常,那么这个问题产生的原因又是什么?

index_stock=request.getParameter("Service");
if (index_stock!=null&index_stock.equals(ConstantConfig.MSG_STOCK_WANT))
//按代码查询股票行情
{
System.out.println("股票行情");
out.write(000);
out.flush();
out.close();
}
Smilings 2003-04-30
  • 打赏
  • 举报
回复
弄好了,原来是我自己在传递参数时出现了错误:)
Smilings 2003-04-30
  • 打赏
  • 举报
回复
通过ie来运行这个程序:
http://localhost:8080/servlet2?str=haha

或:http://localhost:8080/servlet2?
两种情况出现的结果都是一样:
Sorry!
The servlet has received a GET. This is the reply.

但按照正常逻辑,第一种情况应该是:
haha
The servlet has received a GET. This is the reply.


为什么会产生这种情况的呢?




Smilings 2003-04-30
  • 打赏
  • 举报
回复
呵呵,原来是这样的:)

谢谢!
Smilings 2003-04-30
  • 打赏
  • 举报
回复
明白了,谢谢哦!

jspxnet 2003-04-30
  • 打赏
  • 举报
回复
java 当在处理空对象时候就会出这个错, asp,c都不会有这个问题.
感觉很繁人.

注意比较空用:
if (str == null) .....

如果用
str.equals(null)是错的,
因为如果str=nul
那你的语句就变成
null.equals(null)
null是没有什么方法,属性的,就java.lang.NullPointerException了
Smilings 2003-04-30
  • 打赏
  • 举报
回复
呵呵,有点不好意思了。

不过我傻呼呼的弄不明白,只好厚着脸皮来问了:)

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧