0

Good Evening Everyone.

So I have a little problem with List in Java. Let's start from beggining. Here is method which returns list from Query in HQL :

public List<PraktykiEntity> getPracticeOfStudent(Long nrAlbumu) {
        Query query = em.createQuery("SELECT p FROM PraktykiEntity p WHERE p.nrAlbumu=?1");
        query.setParameter(1, nrAlbumu);
        List list = query.getResultList();
        if (!list.isEmpty()) {
            return list;
        } else {
            return null;
        }
    }

then a helper class :

public class PraktykaInfo {

    public Long idPraktykiStudenckiej; //idOfStudentPractice
    public SzablonyPraktykEntity szablonPraktyki; //templateOfPractice
    public PraktykodawcyEntity praktykodawca; //Employer
    public StatusyEntity statusPraktyki; //statusOfPractice
    public List<KierunkiStudiowEntity> kierunek; //Course
    public TypyPraktykEntity typPraktyki; //typeOfPractice
    public LataAkademickieEntity rokAkademicki; //academicYear
    public List<StudenciEntity> student; 
    public List<OpiekunowiePraktykEntity> opiekunPraktyki; //tutor
    public List<KoordynatorzyPraktykEntity> koordynatorPraktyki; //coordinator
    public AdresyEntity adresPracodawcy; //Employer's address
    public PorozumieniaEntity porozumienie; //agreement}

And what i want to do - get List<PraktykiEntity> from first method, then initialize new helper List<PraktykaInfo> and iterate through it, set correct object from first list and then return it. At first i thought list.get(i) will work but im getting error "index out of bounds". I've heard about list.add and .set but don't know how to use them. Here is the method. And i do know that info.get(i) has nothing that's why i am encountering error "index out of bounds"

public List<PraktykaInfo> getPracticeOfStudent(Long nrAlbumu) {
        List<PraktykiEntity> list = ipraktykiDAO.getPracticeOfStudent(nrAlbumu);
        List<PraktykaInfo> info = new ArrayList<PraktykaInfo>();
        //System.out.println(list.size());
        for(int i = 0; i<list.size(); i++) {
            PraktykiEntity practice = ipraktykiDAO.getRow(list.get(i).getIdPraktykiStudenckiej());
            if(practice !=null) {
                info.get(i).setIdPraktykiStudenckiej(practice.getIdPraktykiStudenckiej());
                info.get(i).setSzablonPraktyki(iszablonyPraktykDAO.getTemplate(practice.getIdSzablonu()));
                info.get(i).setPraktykodawca(ipraktykodawcyDAO.findEmployer(practice.getIdPraktykodawcy()));
                info.get(i).setStatusPraktyki(istatusyDAO.getStatus(practice.getIdStatusu()));
                info.get(i).setKierunek(ikierunkiStudiowDAO.getCourseInfo(practice.getIdKierunku()));
                info.get(i).setTypPraktyki(itypyPraktykDAO.getTypeOfTraineeship(practice.getIdTypuPraktyki()));
                info.get(i).setRokAkademicki(ilataAkademickieDAO.getYearOfCourse(practice.getIdRokuAkademickiego()));
                if(practice.getNrAlbumu() !=null) {
                    info.get(i).setStudent(istudenciDAO.getStudentInfo(practice.getNrAlbumu()));
                }
                info.get(i).setOpiekunPraktyki(iopiekunowiePraktykDAO.getTutorInfo(practice.getIdOpiekunaPraktyk()));
                info.get(i).setKoordynatorPraktyki(ikoordynatorzyPraktykDAO.getCoordinatorInfo(practice.getIdKoordynatoraPraktyk()));
                info.get(i).setAdresPracodawcy(iadresyDAO.findByIdAdresu(practice.getIdAdresu()));
                if(practice.getIdPorozumienia() !=null) {
                    info.get(i).setPorozumienie(iporozumieniaDAO.getAgreement(practice.getIdPorozumienia()));
                }
            } else {
                return null;
            }
        }
        return info;
    }
papski
  • 37
  • 1
  • 2
  • 7
  • it looks to me that when you use info.get(i) you are getting index out of bounds because there is never anything in info – JRowan Mar 20 '15 at 21:27
  • Yes, that's true, so how can i set them correctly ? – papski Mar 20 '15 at 21:31
  • make a variable of PraktykaInfo in = new PraktykaInfo(), then where you have info.get(0) use "in".set all you info then add it to the info list – JRowan Mar 20 '15 at 21:32
  • Wow, didn't think of it, such a simple solution, thanks! – papski Mar 20 '15 at 21:38
  • As a side note your use of `null` is really not a good pattern. It's just making you do more work and possibly introduce errors. An empty List already properly indicates 0 results. – Radiodef Mar 20 '15 at 21:38
  • @Radiodef You mean practice.getNrAlbumu() !=null and practice.getIdPorozumienia() !=null ? – papski Mar 20 '15 at 21:41
  • I count 2 `return null`s and 3 `!= null`s. At least one of these pairs (where you check if a List is empty and then return `null` instead) is completely unnecessary because the List is looped over immediately. If you simply removed that conversion to `null`, `info` would also be returned empty. Putting `null` everywhere is a very error-prone way to code. – Radiodef Mar 20 '15 at 21:50
  • See also http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java – Radiodef Mar 20 '15 at 21:51
  • Thanks for useful information, I'll keep that in mind, I've just started my journey with let's say serious programming , accoridng to 2 !=null they're necessary because methods from IF goes to EntityManager.find and when it returns null i do get exception, i can fix that by making new method that returns list from query, but, that IF was simplier and faster to make. – papski Mar 20 '15 at 21:56

2 Answers2

0

You get an IndexOutOfBoundsException because you are calling info.get(i) on an empty list.

You can modify your code to something like this:

public List<PraktykaInfo> getPracticeOfStudent(Long nrAlbumu) {
    List<PraktykiEntity> list = ipraktykiDAO.getPracticeOfStudent(nrAlbumu);
    List<PraktykaInfo> info = new ArrayList<PraktykaInfo>();
    //System.out.println(list.size());
    for(int i = 0; i<list.size(); i++) {
        PraktykiEntity practice = ipraktykiDAO.getRow(list.get(i).getIdPraktykiStudenckiej());
        if(practice !=null) {
            PraktykaInfo pi = new PraktykaInfo();
            pi.setIdPraktykiStudenckiej(practice.getIdPraktykiStudenckiej());
            pi.setSzablonPraktyki(iszablonyPraktykDAO.getTemplate(practice.getIdSzablonu()));
            pi.setPraktykodawca(ipraktykodawcyDAO.findEmployer(practice.getIdPraktykodawcy()));
            pi.setStatusPraktyki(istatusyDAO.getStatus(practice.getIdStatusu()));
            pi.setKierunek(ikierunkiStudiowDAO.getCourseInfo(practice.getIdKierunku()));
            pi.setTypPraktyki(itypyPraktykDAO.getTypeOfTraineeship(practice.getIdTypuPraktyki()));
            pi.setRokAkademicki(ilataAkademickieDAO.getYearOfCourse(practice.getIdRokuAkademickiego()));
            if(practice.getNrAlbumu() !=null) {
                pi.setStudent(istudenciDAO.getStudentInfo(practice.getNrAlbumu()));
            }
            pi.setOpiekunPraktyki(iopiekunowiePraktykDAO.getTutorInfo(practice.getIdOpiekunaPraktyk()));
            pi.setKoordynatorPraktyki(ikoordynatorzyPraktykDAO.getCoordinatorInfo(practice.getIdKoordynatoraPraktyk()));
            pi.setAdresPracodawcy(iadresyDAO.findByIdAdresu(practice.getIdAdresu()));
            if(practice.getIdPorozumienia() !=null) {
                pi.setPorozumienie(iporozumieniaDAO.getAgreement(practice.getIdPorozumienia()));
            }
            info.add(pi);
        } else {
            return null;
        }
    }
    return info;
}
Titus
  • 22,031
  • 1
  • 23
  • 33
  • That's what JRowan suggested, it works, such a simple solution but didn't think about it. Thanks for answers Guys! – papski Mar 20 '15 at 21:39
0

You have to add something to the List to get something out of it. You can only use the setters on get(i) if there is something at i. A better approach would be to create the full object with setters and then add it to the List.

PraktykaInfo pi = new PraktykaInfo();
pi.set...
...
info.add(pi);
tinker
  • 1,396
  • 11
  • 20