jsp处理javaBean出错 为什么没人回答呢?我把allcfgItem[i].setItem_id(cfg_id);放在代码中为什么会出现异常:null 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把allcfgItem[i].setItem_id(cfg_id);放在代码中为什么会出现异常:null说明你没有取到数据,所以不能执行操作啊!弄的具体点,把错误也贴出来! error at getItem_cfg_tablemessage!null这是我打印出来的异常我的javaBean中只写了get_()和set_()方法,可以吗?可是有那为能帮我分析一下为什么会取不到数据呢? i在这里只是个记数的变量int i=0;//用来标记Item_cfgtable[] allcfgItem 的下标while(rs.next()){----i+1;}就这样 Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");//在这个页面取得的项目的具体信息将用session值保存String cfg_name="",cfg_open="",cfg_open_name="",cfg_type="",cfg_type_name="",union_sp_n="",mobile_sp_n="",phs_sp_n="",cfg_commd="",msg_content1="",msg_content2="",msgcontentnot=""; int cfg_id;try{ ConnectionData connectionDB=new ConnectionData(Parameter.jdbcName,Parameter.qu_url,Parameter.qu_user,Parameter.qu_password); ResultSet rs=null; String sql="select * from item_cfg_table where group_id='"+g_id+"'"; rs=connectionDB.get_query_Result(sql); //System.out.println("ok select!"); int i=0;//用来标记Item_cfgtable[] allcfgItem 的下标 while(rs.next()){ cfg_id=rs.getInt("item_id"); //项目ID allcfgItem[i].setItem_id(cfg_id); cfg_name=rs.getString("item_name"); //项目名称 allcfgItem[i].setItem_name(cfg_name); cfg_open=rs.getString("open_flag"); //开放标志 allcfgItem[i].setOpen_flag(cfg_open); cfg_type=rs.getString("item_type"); //项目类型 allcfgItem[i].setItem_type(cfg_type); union_sp_n=rs.getString("union_sp_number");//联通特服号 allcfgItem[i].setUnion_sp_number(union_sp_n); mobile_sp_n=rs.getString("mobile_sp_number");//移动特服号 allcfgItem[i].setMobile_sp_number(mobile_sp_n); phs_sp_n=rs.getString("phs_sp_number"); //小灵通特服号 allcfgItem[i].setPhs_sp_number(phs_sp_n); cfg_commd=rs.getString("msg_commd"); //点播指令 allcfgItem[i].setMsg_commd(cfg_commd); msg_content1=rs.getString("msg_content1"); //反馈内容1 allcfgItem[i].setMsg_content1(msg_content1); msg_content2=rs.getString("msg_content2"); //反馈内容2 allcfgItem[i].setMsg_content2(msg_content2); msgcontentnot=rs.getString("msgcontentnot"); //错误反馈 allcfgItem[i].setMsgcontentnot(msgcontentnot); i=i+1;//Item[] allItem 的下标增 1 } session.setAttribute("allcfgItem",allcfgItem);//将修改后的allItem (取得了每个项目具体的信息)仍旧用allItem保存在session中,在后面的页面中只 //要从session 中取值就行,而不需要再查数据库 rs.close(); connectionDB.close_Statement(); connectionDB.close_con(); }catch(Exception e){ out.println("error at getItem_cfg_tablemessage!"+e.getMessage()); }有劳了!! 估计错误出在Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");即allcfgItem并没有取到值,或是allcfgItem的length小于你查询出来的记录数,所以可能是数组越界异常。catch(Exception e){ out.println("error at getItem_cfg_tablemessage!"+e.getMessage());}你这样的语句有些问题,因为异常并不是你手工抛出的,所以这样写实际上根本得不到任何对异常信息的描述,不如用e.printStackTrace()显示异常信息,至少也在前面加上这么一句,不然调试很困难的。 楼上的大哥,我按你所提示的用e.printStackTrace()将异常打印出来,得到的是一条java.lang.nullpointerException:的异常消息,那就是说我所调用的是一条空指针对象了,后来我将Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");改为:Item_cfgtable[] allcfgItem=new Item_cfgtable[15];也得到了同样的结果问题就是我的同事用同样的方法(调用Bean类不同)却能够实现功能,他的代码是: Item[] allItem=(Item[])session.getAttribute("allItem");这又怎么解释呢,难道跟Bean也有关系?请大哥帮忙指点!! 请大家关注:http://community.csdn.net/Expert/topic/3209/3209605.xml :(问题的关键是,你到底在session里放了什么?如果session里保存了叫做allItem的Item数组,那么用Item[] allItem=(Item[])session.getAttribute("allItem");当然就可以取到啊你用Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");不能正确地取到想要的Item_cfgtable数组,很可能是因为session里没有叫做allcfgItem的Item_cfgtable数组ps:Item_cfgtable[] allcfgItem=new Item_cfgtable[15];只是分配了可以存放15个Item_cfgtable的空间而已你并没有初始化这个数组的值,所以照样会有NullPointerException 大哥我明白了,我的Bean里面确实没有先从数据库中取数进行初始话,也就是说session里根本就没有东西。我现在就去试一试,如果能够成功,结贴是一定付帐 ): apache 负载FTP,设置FTP目录的访问权限 求教一个jsp+javabean的问题 关于mysql的一些问题 jsp当中el表达式不起作用! 基础问题,字符串比较为什么不真? JSP参数转换出错. 谁知道servlet的RequestDisPatcher的用法:帮我看看程序 Spring cache不起作用 急问!!!! 何解决servlet运行出现的“????”,该怎么设置啊? mysql的严重问题,你行吗? 在JBuilder中,我使用JdbTable控件连接mysql数据库,出现中文的地方全是乱码,请问如何解决。
这是我打印出来的异常
我的javaBean中只写了get_()和set_()方法,可以吗?
可是有那为能帮我分析一下为什么会取不到数据呢?
int i=0;//用来标记Item_cfgtable[] allcfgItem 的下标
while(rs.next()){
----
i+1;
}
就这样
//在这个页面取得的项目的具体信息将用session值保存
String cfg_name="",cfg_open="",cfg_open_name="",cfg_type="",cfg_type_name="",union_sp_n="",mobile_sp_n="",phs_sp_n="",cfg_commd="",msg_content1="",msg_content2="",msgcontentnot="";
int cfg_id;
try{
ConnectionData connectionDB=new ConnectionData(Parameter.jdbcName,Parameter.qu_url,Parameter.qu_user,Parameter.qu_password);
ResultSet rs=null;
String sql="select * from item_cfg_table where group_id='"+g_id+"'";
rs=connectionDB.get_query_Result(sql);
//System.out.println("ok select!");
int i=0;//用来标记Item_cfgtable[] allcfgItem 的下标
while(rs.next()){
cfg_id=rs.getInt("item_id"); //项目ID
allcfgItem[i].setItem_id(cfg_id);
cfg_name=rs.getString("item_name"); //项目名称
allcfgItem[i].setItem_name(cfg_name);
cfg_open=rs.getString("open_flag"); //开放标志
allcfgItem[i].setOpen_flag(cfg_open);
cfg_type=rs.getString("item_type"); //项目类型
allcfgItem[i].setItem_type(cfg_type);
union_sp_n=rs.getString("union_sp_number");//联通特服号
allcfgItem[i].setUnion_sp_number(union_sp_n);
mobile_sp_n=rs.getString("mobile_sp_number");//移动特服号
allcfgItem[i].setMobile_sp_number(mobile_sp_n);
phs_sp_n=rs.getString("phs_sp_number"); //小灵通特服号
allcfgItem[i].setPhs_sp_number(phs_sp_n);
cfg_commd=rs.getString("msg_commd"); //点播指令
allcfgItem[i].setMsg_commd(cfg_commd);
msg_content1=rs.getString("msg_content1"); //反馈内容1
allcfgItem[i].setMsg_content1(msg_content1);
msg_content2=rs.getString("msg_content2"); //反馈内容2
allcfgItem[i].setMsg_content2(msg_content2);
msgcontentnot=rs.getString("msgcontentnot"); //错误反馈
allcfgItem[i].setMsgcontentnot(msgcontentnot);
i=i+1;//Item[] allItem 的下标增 1
} session.setAttribute("allcfgItem",allcfgItem);//将修改后的allItem (取得了每个项目具体的信息)仍旧用allItem保存在session中,在后面的页面中只
//要从session 中取值就行,而不需要再查数据库
rs.close();
connectionDB.close_Statement();
connectionDB.close_con();
}catch(Exception e){
out.println("error at getItem_cfg_tablemessage!"+e.getMessage());
}
有劳了!!
Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");即allcfgItem并没有取到值,或是allcfgItem的length小于你查询出来的记录数,所以可能是数组越界异常。catch(Exception e){
out.println("error at getItem_cfg_tablemessage!"+e.getMessage());
}
你这样的语句有些问题,因为异常并不是你手工抛出的,所以这样写实际上根本得不到任何对异常信息的描述,不如用e.printStackTrace()显示异常信息,至少也在前面加上这么一句,不然调试很困难的。
java.lang.nullpointerException:的异常消息,
那就是说我所调用的是一条空指针对象了,后来我将Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");改为:Item_cfgtable[] allcfgItem=new Item_cfgtable[15];也得到了同样的结果
问题就是我的同事用同样的方法(调用Bean类不同)却能够实现功能,他的代码是:
Item[] allItem=(Item[])session.getAttribute("allItem");
这又怎么解释呢,难道跟Bean也有关系?
请大哥帮忙指点!!
http://community.csdn.net/Expert/topic/3209/3209605.xml
如果session里保存了叫做allItem的Item数组,那么用
Item[] allItem=(Item[])session.getAttribute("allItem");
当然就可以取到啊你用
Item_cfgtable[] allcfgItem=(Item_cfgtable[])session.getAttribute("allcfgItem");
不能正确地取到想要的Item_cfgtable数组,很可能是因为session里没有叫做allcfgItem的Item_cfgtable数组
ps:
Item_cfgtable[] allcfgItem=new Item_cfgtable[15];
只是分配了可以存放15个Item_cfgtable的空间而已
你并没有初始化这个数组的值,所以照样会有NullPointerException
我现在就去试一试,如果能够成功,结贴是一定付帐 ):