1

I have method which returns List of objects, I m facing a problem while fetching a data from list, I m getting null pointer exception when i am trying to fetch the data.

method which returns List:

public List <EstimationResivion> getEstimationRevision(double rev_id){

List<EstimationDetailRevision>  estDetail=estDetailRev(rev_id);
List<EstimationPanelRev> estPanelrev=estPanelRev(rev_id);
List<EstimationPannelDetailRevision>   estimationpaneldetailrev=estPanelDetailRev(rev_id);
List<EstimationResivion> estRev=estRevision(rev_id);

    EstimationResivion ed= new EstimationResivion();
    ed.setEstimationdetailRev(estDetail);
    ed.setEstimationpanelRev(estPanelrev);
    ed.setEstimationpaneldetailRev(estimationpaneldetailrev);

    System.out.println("Size before: "+estRev.size());
    estRev.add(ed);

    return estRev;
    }

estDetailRev code:

private List<EstimationDetailRevision> estDetailRev(double d){

    return getJdbcTemplate().query("SELECT * FROM estimation_detail_rev WHERE rev_id=?",  new Object[] {d }, new EstimationDetailRevRowMapper());
}

estRevision code

private List<EstimationResivion> estRevision(double d){

    return getJdbcTemplate().query("SELECT * FROM estimation WHERE est_revision=?",  new Object[] {d }, new EstimationRevRowMapper());

}

estRevision will return list of List<EstimationResivion> which i am storing in estRev.

code from where i am calling getEstimationRevision and trying to fetch data from list

public void getRevision(){
double d=0;
List<EstimationResivion> est=estimationdao.getEstimationRevision(d);

for(EstimationResivion er:est){
    System.out.println(er.getEstContactPerson());
    System.out.println(er.getEstCustomer());
    System.out.println(er.getRevId());
    // epd=er.getEstimationpaneldetailRev();

    List<EstimationDetailRevision>  edr=er.getEstimationdetailRev();
    for(EstimationDetailRevision estimaitonDetail:edr){

        estimaitonDetail.getCompCategory();
        estimaitonDetail.getCompMake();
    }

}

I am getting null value when i call er.getEstimationdetailRev(); if i specify the index i am able to getdata something like this:

List<EstimationDetailRevision>  edr=est.get(64).getEstimationdetailRev();

My question is how can i fetch data without specifying index ?

user3844950
  • 71
  • 1
  • 11
  • 1
    so its looks like element `64` has non-null `getEstimationdetailRev` but some other element has null `getEstimationdetailRev` – Scary Wombat Jan 30 '15 at 05:02
  • Really hard to answer this when you haven't shown us all of the code. But what is the return type of `getEstimationpaneldetailRev` and what is the declared type of `epd`? It's possible that this is being caused by the auto-unboxing of a null. – Dawood ibn Kareem Jan 30 '15 at 05:16
  • I think i am facing this problem because in `getEstimationRevision` method at the end i am adding `ed` object to `estRev` `( estRev.add(ed);)` i.e at 64th position so i am able to fetch data from index 64. my Issue is i dont want to specify the index, i want to run the forloop and fetch the data – user3844950 Jan 30 '15 at 05:51

1 Answers1

1

One of your elements is null (not a value). Let's find out which,

// for(EstimationResivion er:est){
for (int index = 0; index < est.size(); index++) {
    EstimationResivion er = est.get(index);
    if (er == null) {
        System.out.println("element at index " + index + " is null");
        continue; // <-- move to the next index
    }
    // ...
    List<EstimationDetailRevision>  edr=er.getEstimationdetailRev();
    if (edr == null) {
        System.out.println("edr at index " + index + " is null");
        continue; // <-- move to the next index
    }

Also, you could (and should) use a debugger on your code.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks for your answer. But my issue is i am calling `List estRev=estRevision(rev_id);` now method `estRevision` will return `List` which i am storing in `estRev`. now `estRev` contain 63 elements. next i am adding 64th element to `estRev` something like this `estRev.add(ed);`.now estRev contains 63 elements + `ed` as 64th element. how can i access 64th element without specifying index. and i dont have any null element between 0 to 63. – user3844950 Jan 30 '15 at 10:06