我从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[]为空  我怎么样才能给他负个初值呢 或者有别的办法  求解答

解决方案 »

  1.   


    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());
      //...
      

  2.   

    CardFormated[] cardFormateds = new CardFormated[total]
    只创建了一个数组,数组元素预计是CardFormated对象,但目前还都是null,即CardFormated[i]都是null之后,你没有给CardFormated[i]初始化就开始调用CardFormated[i].set...,所以报空指针错误
      

  3.   

    CardFormated[] cardFormateds = new CardFormated[total]
    只创建了一个数组,数组元素预计是CardFormated对象,但目前还都是null,即CardFormated[i]都是null之后,你没有给CardFormated[i]初始化就开始调用CardFormated[i].set...,所以报空指针错误
      

  4.   

    CardFormated[] cardFormateds = new CardFormated[total];
    这是创建数组,数组内的元素默认都是null。你需要在调用cardFormateds[i]的方法之间进行附值:
    cardFormateds[i] = new CardFormated();
    cardFormateds[i].setAccountId(cardWithList.get(i).getAccountId());
    ⋯⋯
      

  5.   

    1楼正解,你只是定义好了数组但是没往数组里面放对象,所以在用cardFormateds[i]调方法的时候会报空指针。
      

  6.   

    楼上正解!你CardFormated[] cardFormateds = new CardFormated[total];只是new了一群CardFormated的引用,所以cardFormateds 数组中的每个元素初始化的值都是null,所以报了空指针!
      

  7.   

    你使用
    List的toArray方法,就可以直接转化成数组了
      

  8.   


    Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
    if(cardWithSession != null){
    List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
    CardFormated[] cardFormateds = cardWithList.toArray();
      

  9.   


    Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
    if(cardWithSession != null){
    List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
    CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
    忘了还要转个型,sorry
      

  10.   


    Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
    if(cardWithSession != null){
    List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
    CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
    忘了还要转个型,sorry
      

  11.   


    Object cardWithSession = getSession().getAttribute("CARD_WITH_RESULT");
    if(cardWithSession != null){
    List<CardWithColumn> cardWithList = (ArrayList)cardWithSession;
    CardFormated[] cardFormateds = (CardFormated[])cardWithList.toArray();
    忘了还要转个型,sorry