close nav

close nav

Thread: ResultSet Extractor i am getting null pointer exception how can i solve

Forum : Spring Jdbc, Spring ORM and Spring Transaction.   9 Days ago
User Image
PurushRedd

Newbie

Joined: 24 Nov, 2020

Points: 100

Threads: 1

Posts: 0

ResultSet Extractor i am getting null pointer exception how can i solve

| Quote Date : 2 May, 2021    Views:107    



package com.qa.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.qa.bo.CustomerBo;
import com.qa.config.PersistanceConfig;
import com.qa.dao.BikeDao;
import com.qa.dao.CustomerDao;
public class QaTest {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(PersistanceConfig.class);
        //BikeDao bikeDao = context.getBean("bikeDao",BikeDao.class);
        /*
         * int nbikes = bikeDao.getNoOfBikes(); System.out.println("n bikes : "+nbikes);
         * 
         * //String rta = bikeDao.getRtaRegNO(101); //System.out.println(rta);
         * 
         * BikeBo bikeBo = bikeDao.getBik(101); System.out.println(bikeBo);
         * 
         * List<BikeBo> listOfManuFactBikes =
         * bikeDao.getBikesByManufacturer("hero-honda");
         * listOfManuFactBikes.forEach(System.out::println)
         */;
        CustomerDao customerDao = context.getBean("customerDao", CustomerDao.class);
        List<CustomerBo> customers = customerDao.CustomerWithRentalDetails();
        customers.forEach(System.out::print);
    }
}


package com.qa.bo;
import java.util.Date;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import oracle.jdbc.proxy.annotation.Pre;
@AllArgsConstructor
@Data
public class CustomerBo {
    @NonNull
    private  String customerNo;
    @NonNull
    private String fullname;
    private Date dob;
    @NonNull
    private String mobileNo;
    @NonNull
    private String emailAddress;
    private String drivingLicenseNo;
    @NonNull
    private List<BikeRentalBo> bikeRentals;
}


package com.qa.bo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
 * @author purushotham reddy
 *
 */
@Setter
@Getter
@AllArgsConstructor
@ToString
public class BikeBo {
    private int bikeNo;
    private String bikeModelName;
    private String manufacturer;
    private String rtaRegistrationNo;
    private double price;
}


package com.qa.bo;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@AllArgsConstructor
@ToString
public class BikeRentalBo {
    @NonNull
    private String rtaRegistrationNo;
    @NonNull
    private String bikeModel;
    @NonNull
    private Date bikeRentDate;
    @NonNull
    private Integer hours;
    @NonNull
    private Double amountPaid;
}


package com.qa.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;
import com.qa.bo.BikeRentalBo;
import com.qa.bo.CustomerBo;
@Repository
public class CustomerDao {
    private final String SQL_GET_CUSTOMER_AND_BIKE_RENTS ="select c.customer_no, c.full_nm,c.mobile_no,c.email_address, b.rtaregistrationno,b.bikemodelname,br.rented_dt,br.duration,br.amount_paid from  bike b inner join bike_rental br on b.bikeno = br.bike_rental_no inner join customerr c on c.customer_no = br.customer_no order by customer_no";
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public List<CustomerBo> CustomerWithRentalDetails(){
        return jdbcTemplate.query(SQL_GET_CUSTOMER_AND_BIKE_RENTS,new CustomerBoResultSetExtractor());
    }
    private final class CustomerBoResultSetExtractor implements ResultSetExtractor<List<CustomerBo>> {
        @Override
        public List<CustomerBo> extractData(ResultSet rs) throws SQLException, DataAccessException {
            Map<Integer,CustomerBo> customerMap = null;
            List<BikeRentalBo> bikeRentals = null;
            List<CustomerBo> customers = null;
            BikeRentalBo bikeRentalBo = null;
            CustomerBo customerBo = null;
            Integer customerNo = 0;
            customerMap = new HashMap<>();
            while(rs.next()) {
                customerNo = rs.getInt(1);
                if(customerMap.containsKey(customerNo)==false) {
                    customerBo = new CustomerBo(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4));
                    bikeRentalBo = new BikeRentalBo(rs.getString(5),rs.getString(6),rs.getDate(7),rs.getInt(8),rs.getDouble(9));
                    bikeRentals = new ArrayList<BikeRentalBo>();
                    bikeRentals.add(bikeRentalBo);
                    customerBo.setBikeRentals(bikeRentals);
                }
                customerBo = customerMap.get(customerNo);
                bikeRentalBo = new BikeRentalBo(rs.getString(5),rs.getString(6),rs.getDate(7),rs.getInt(8),rs.getDouble(9));
                customerBo.getBikeRentals().add(bikeRentalBo);
            }
            customers = customerMap.values().stream().collect(Collectors.toList());
            return customers;
        }
    }
}

Comments

User Image
Hariom

Newbie

Joined : 8 Oct, 2020

Points : 100

Threads: 0

Posts: 5

Re: ResultSet Extractor i am getting null pointer exception how can i solve

Reply Date : 2 May, 2021

@purushredd you need create empty arraylist object in while condition and set that ArrayList to customerBo.setBikeRental().. in customerBo the value of List<BikeRentalBo> bikeRentalBo; is null therefore u are getting null pointer exception

Load More

No More Comments