1

I have a problem with useing NULL as Integer parameter.

I have 3 classes which extend each other:

//1st
public class StandardTableIntId {
    ...

    protected MethodAnswer changeField(String field, Integer value) {
    DBConnection dbc = new DBConnection();
    Connection con = dbc.getConnection();
    try {
        PreparedStatement st = con.prepareStatement("UPDATE " + this.tableName + " SET " + field + " = ? WHERE id = ?");
        st.setInt(1, value);
        st.setInt(2, this.id);
        st.executeUpdate();
        return MethodAnswer.OK;
    } catch (SQLException ex) {
        log.info("SQLException. Class: StandardTableIntId. Method: changeField(String field, String value). Parameters: table='" + tableName + "' id='" + id + "' field='" + field + "' value='" + value + "'");
        return MethodAnswer.DB_EXCEPTION;
    }
    finally
    {
        dbc.closeConnection();
    }   
    }
    protected MethodAnswer changeField(String field, String value) {
    ...
    st.setString(1, value);
    ...}
    protected MethodAnswer changeField(String field, Long value) {
    ...
    st.setLong(1, value);
    ...}
    protected MethodAnswer changeField(String field, boolean value) {
    ...
    st.setBoolean(1, value);
    ...}
    ...
}   


//2nd
public class StandardTableIntIdTimed extends StandardTableIntId{...}

//3rd   
public class Company extends StandardTableIntIdTimed {
    ...
    public MethodAnswer setMainIncomeBankAccountId(Integer value) {
        MethodAnswer ans;
// !!! Code crashes in next command (if value==null). Eclipse just said: "Source not found." in debugging mode.
            ans = changeField("mainIncomeBaId", value); 
        return ans;
    }
    ...
}

Code crashes in line that marked "//!!!" comment. It happens when "value" is equals to null.

In the same methods but String or Timestamp params is everithing OK:

public MethodAnswer setPostAddress(String postAddress) {
    MethodAnswer ans;
    ans = changeField("postAddress", value); 
    return ans;
}
public MethodAnswer setDateTimeBegin(Timestamp value) {
    MethodAnswer ans;
    ans = changeField("DateTimeBegin", value); 
    return ans;
}

Can't understend what's happening here and how to fix it.

4
  • 1
    Please show a short but complete program demonstrating the problem. Commented Oct 23, 2012 at 14:07
  • and maybe a stacktrace as well Commented Oct 23, 2012 at 14:07
  • 3
    where is the code for changeField(...) ? Commented Oct 23, 2012 at 14:08
  • Add changeField(...) code. All methods do the same thing put one value in DB. It's just different values String/Integer/etc Commented Oct 23, 2012 at 15:03

2 Answers 2

1

Ok, I'm guessing a bit here, but...

protected MethodAnswer changeField(String field, Integer value)

If you within this method convert the value parameter to an int, and value is NULL, then you will get a NullPointerException. The solution: Check if value is NULL before converting it to int.

Sign up to request clarification or add additional context in comments.

Comments

0

You probably are doing some calculations using value. Since value is null the application throws a NullPointerException

EG:

public void test(Integer n){
  System.out.println(n + 4);
}

With the code above, the following code will throw a NullPointerException

test(null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.