0

I have a strange behavior :

I have a method which get some data from a database

public void calculate(Run run, Map<String, Curve> epe) {
    for (Entry<String, Curve> entry : epe.entrySet()) {
        Entity ntt = leDAO.getById(entry.getKey(), run);

        double lgd = leDAO.getLgd(ntt, run);
        Curve dp = curveDAO.getDp(entry.getKey(), run);
        String currency = leDAO.getCurrency(ntt, run);
        Curve df = curveDAO.getDf(currency, run);

        double val = 0.0;
        for (int i = 0; i < pd.size(); i++) {
            cva += entry.getValue().get(i) * df.get(i) * dp.get(i);
        }
        val *= lgd;
    }
}

When debugging, halfway through the outer loop, it stops on this line, although I have no breakpoints set :

double lgd = leDAO.getLgd(ntt, run);

and then, it throws a NullPointerException, except I can't find what's null (leDAO, ntt and run are all set to something other than null). I can't step into the getLgd() method (I get in dispatchUncaughtException() from Thread.class if I do). The map has about 150 keys.

EDIT The getLgd() method :

public Double getLgd(Entity entity, Run run) throws HibernateException {
    if (session == null) {
        session = sessionFactory.openSession();
    }
    SQLQuery select = session.createSQLQuery(RECOVERY_SELECT);
    select.setLong(0, run.getRunId());
    select.setString(1, entity.getId());
    BigDecimal result = (BigDecimal) select.uniqueResult();
    Double lgd = null;
    if (result != null) {
        entity.setRr(result);
        BigDecimal one = new BigDecimal(1);
        BigDecimal normalLgd = one.subtract(result);
        lgd = normalLgd.doubleValue();
    }
    return lgd;
}
l0r3nz4cc10
  • 1,237
  • 6
  • 27
  • 50
  • what exactly leDAO.getLgd(ntt, run) do? Can you provide this method – Kick Feb 03 '14 at 17:04
  • Your code seems to be suffering from the [n+1 selects issue](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue). In your case, it's actually a 5n+1 issue ;) – lance-java Feb 03 '14 at 17:08

1 Answers1

1

Here is a potential cause for a null pointer exception:

public Double getLgd(... stuff)
{
    Double lgd = null;
    if (result != null)
    {
        ... blah
    }

    return lgd;
}


double ILikeNullPointerExceptionsWhenResultIsNull = getLgd();

Explaination

Autobox conversion from a Double value to a double value includes an implicit dereference of the Double value. For example:

Double blam = null;
double hoot;

hoot = blam;

The statement hoot = blam is no different from hoot = blam.doubleValue(). The dereference of the blam variable above will cause a NullPointerException.

DwB
  • 37,124
  • 11
  • 56
  • 82