我从session中得到一个数组的值 然后我想把里面的值都负值给我自己定义的一个数组 代码如下:
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
int total = cardWithList.size();
CardFormated[] cardFormateds = new CardFormated[total];
for(int i = 0; i < total; i++){
System.out.println(cardWithList.get(i).getAccountId());
cardFormateds[i].setAccountId(cardWithList.get(i).getAccountId());
cardFormateds[i].setCardID(cardWithList.get(i).getCardID());
cardFormateds[i].setCardType(cardWithList.get(i).getCardType());
cardFormateds[i].setCost(cardWithList.get(i).getCost());
cardFormateds[i].setFirstFlag(cardWithList.get(i).getFirstFlag());
cardFormateds[i].setPhysicalId(cardWithList.get(i).getLongPhysicalId());
cardFormateds[i].setStatus(CardStateEnum.NormalCard);
}
accountFormated.setCards(cardFormateds);
}可是总提示我空指针 就是cardFormated[]为空 我怎么样才能给他负个初值呢 或者有别的办法 求解答
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
int total = cardWithList.size();
CardFormated[] cardFormateds = new CardFormated[total];
for(int i = 0; i < total; i++){
System.out.println(cardWithList.get(i).getAccountId());
cardFormateds[i].setAccountId(cardWithList.get(i).getAccountId());
cardFormateds[i].setCardID(cardWithList.get(i).getCardID());
cardFormateds[i].setCardType(cardWithList.get(i).getCardType());
cardFormateds[i].setCost(cardWithList.get(i).getCost());
cardFormateds[i].setFirstFlag(cardWithList.get(i).getFirstFlag());
cardFormateds[i].setPhysicalId(cardWithList.get(i).getLongPhysicalId());
cardFormateds[i].setStatus(CardStateEnum.NormalCard);
}
accountFormated.setCards(cardFormateds);
}可是总提示我空指针 就是cardFormated[]为空 我怎么样才能给他负个初值呢 或者有别的办法 求解答
解决方案 »
- 求助。。一段html代码效果,怎么把它变成一张图片!
- 表单验证的问题
- 100分赠送 解决就给分 在线等!!!!!
- jsp用什么方法,获得该网站打开的来源页面的地址。
- jsp 用户退出问题
- 光盘制作,急用?
- 哪位大哥有关于tapestry的demo给我一个.谢谢~~~~~!
- 在java中如何得到当前代码所在的行号?
- 请问搞jsp算不算是java程序员
- HTTP Status 404 - /Struts_HelloWorld/HelloWorld.jsp错误怎么解决
- 我的myeclipse8.5中无法启动tomcat6.0,但是单独使用tomcat6.0能正常使用的。在myeclipse中启动报错如下。请高手指点……
- org.apache.tomcat.dbcp.dbcp.SQLNestedException
for(int i = 0; i < total; i++){
System.out.println(cardWithList.get(i).getAccountId());
cardFormateds[i] = new CardFormated(); //在此处加上这句
cardFormateds[i].setAccountId(cardWithList.get(i).getAccountId());
//...
只创建了一个数组,数组元素预计是CardFormated对象,但目前还都是null,即CardFormated[i]都是null之后,你没有给CardFormated[i]初始化就开始调用CardFormated[i].set...,所以报空指针错误
只创建了一个数组,数组元素预计是CardFormated对象,但目前还都是null,即CardFormated[i]都是null之后,你没有给CardFormated[i]初始化就开始调用CardFormated[i].set...,所以报空指针错误
这是创建数组,数组内的元素默认都是null。你需要在调用cardFormateds[i]的方法之间进行附值:
cardFormateds[i] = new CardFormated();
cardFormateds[i].setAccountId(cardWithList.get(i).getAccountId());
⋯⋯
List的toArray方法,就可以直接转化成数组了
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
CardFormated[] cardFormateds = cardWithList.toArray();
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
忘了还要转个型,sorry
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
忘了还要转个型,sorry
Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
if(cardWithSession != null){
List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
忘了还要转个型,sorry