列表<a> into List<b> java</b></a>

时间:2014-02-24 21:45:48

标签: java spring hibernate

  

线程“main”java.lang.NullPointerException中的异常   在com.business.impl.MeteobussinesImpl.afficherMeteo1(MeteobussinesImpl.java:34)   在com.test.Tester.main(Tester.java:32)   1.Main Class       package com.test;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.business.Meteobussines;
import com.business.impl.MeteobussinesImpl;
import com.model.vo.Meteo;

public class Tester {
    static public void displayList(List list) {
        Iterator iter = list.iterator();
        if (!iter.hasNext()) {
            System.out.println("La lsite est vide");
            return;
        }
        while (iter.hasNext()) {
            Meteo ct = (Meteo) iter.next();
            System.out.println("tempsMax :" + ct.getTempMax() + " pays :" + ct.getLibilePays() + "  distination :" + ct.getLibileDistination());
        }
    }

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        String[] configArray = new String[]{"/com/resource/spring/ApplicationContext.xml",
                "/com/resource/spring/ApplicationContextDao.xml", "/com/resource/spring/ApplicationContextBusiness.xml"};

        ApplicationContext ctx = new ClassPathXmlApplicationContext(configArray);

        Meteobussines mete = (Meteobussines) ctx.getBean("MeteoBuss");
        List<Meteo> m = mete.afficherMeteo1("tounes");
        displayList(m);

    }

}

2.MeteoBussiness

package com.business.impl;

import java.util.Iterator;
import java.util.List;

import com.business.Meteobussines;
import com.dao.HistoriqueDao;
import com.dao.impl.HistoriqueDaoImpl;
import com.model.dto.Historique;
import com.model.vo.Meteo;

public class MeteobussinesImpl implements Meteobussines {

    HistoriqueDao historiqueDao;

    @SuppressWarnings("null")
    @Override
    public List<Meteo> afficherMeteo1(String pays) {
        List<Historique> hiss = historiqueDao.rechercher(pays);
        List<Meteo> m = null;
        Iterator iter = hiss.iterator();
        if (!iter.hasNext()) {
            System.out.println("La lsite est vide");
        }
        while (iter.hasNext()) {

            Historique ct = (Historique) iter.next();
            Meteo me = new Meteo();
            me.setDateHis(ct.getDateHis());
                /*me.setLibileDistination(ct.getDistination().getLibileDis());
                me.setLibilePays(ct.getPays().getLibilePays());
                me.setLibileVille(ct.getVille().getLibileVille());*/
            me.setTempMax(ct.getTempMax());
            me.setTempMin(ct.getTempMin());
            m.add(me);

        }
        return m;
        // TODO Auto-generated method stub

    }

    @Override
    public List<Meteo> afficherMeteo2(String pays, String ville) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Meteo> afficherMeteo3(String pays, String ville,
                                      String distination) {
        // TODO Auto-generated method stub
        return null;
    }

    public HistoriqueDao getHistoriqueDao() {
        return historiqueDao;
    }

    public void setHistoriqueDao(HistoriqueDao historiqueDao) {
        this.historiqueDao = historiqueDao;
    }

}

1 个答案:

答案 0 :(得分:2)

您在第20行将m设置为null,然后尝试在第34行调用方法,而不会为其指定实际值。

我认为你想在第20行做的是将m初始化为

List<Meteo> m = new ArrayList<Meteo>();