question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

REAL or INTEGER column values from db that are NULL, getter returns 0.0 or 0

See original GitHub issue

DBFlow Version: 4.0.3 Issue Kind (Bug, Question, Feature): Bug

Description: When I do a Select on a table with columns REAL or INTEGER that are nullable, if the row values are NULL for those columns when I call the getter for the values of those columns I get 0 by default instead of NULL. This issue only occurs to me after I migrated from dbflow 3.1.1 to 4.0.3.

Example Table:

@Table(name = "schedule_work", 
database = ISDatabase.class, 
primaryKeyConflict = ConflictAction.REPLACE, 
insertConflict = ConflictAction.REPLACE, 
updateConflict = ConflictAction.REPLACE)
public class ScheduleWork extends BaseModel {

    @PrimaryKey
    @Column(name = "schedule_work_id")
    @SerializedName("schedule_work_id")
    @Expose
    private Integer scheduleWorkId;

    @Column(name = "work_name")
    @SerializedName("work_name")
    @Expose
    private String workName;

    @Column(name = "failure_id")
    @SerializedName("failure_id")
    @Expose
    private Integer failureId;

    /**
     * No args constructor for use in serialization
     */
    public ScheduleWork() {
        super();
    }

    /**
     * @return The scheduleWorkId
     */
    public Integer getScheduleWorkId() {
        return scheduleWorkId;
    }

    /**
     * @param scheduleWorkId The schedule_work_id
     */
    public void setScheduleWorkId(Integer scheduleWorkId) {
        this.scheduleWorkId = scheduleWorkId;
    }

    public ScheduleWork withScheduleWorkId(Integer scheduleWorkId) {
        this.scheduleWorkId = scheduleWorkId;
        return this;
    }

    /**
     * @return The workName
     */
    public String getWorkName() {
        return workName;
    }

    /**
     * @param workName The work_name
     */
    public void setWorkName(String workName) {
        this.workName = workName;
    }

    public ScheduleWork withWorkName(String workName) {
        this.workName = workName;
        return this;
    }

    public Integer getFailureId() {
        return failureId;
    }

    public void setFailureId(Integer failureId) {
        this.failureId = failureId;
    }
}

The query:

new Select().from(ScheduleWork.class)
    .where(ScheduleWork_Table.failure_id.withTable().isNull()).async()
    .queryListResultCallback(callback).execute();

On the callback I call getFailureId() on an entry and I obtain 0 instead of null. Or just by doing:

List<ScheduleWork> swl = new Select().from(ScheduleWork.class)
    .where(ScheduleWork_Table.failure_id.isNull()).queryList();
for(ScheduleWork sw : swl)
    Log.e("Debug", "MY FAILURE_ID: "+sw.getFailureId());

I get the output: MY FAILURE_ID: 0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
olehbozhkocommented, Jun 8, 2017

I found a good temporary solution while awaiting to fix it on DbFlow lib. developers side. I added four converters to avoid using standard functionality. It is also possible to use String in converters as db type but in this case is possible to face problem with Primary keys and default values.

@com.raizlabs.android.dbflow.annotation.TypeConverter
public class IntegerConverter extends TypeConverter<Integer, Integer> {

    @Override
    public Integer getDBValue(Integer model) {
        return model;
    }

    @Override
    public Integer getModelValue(Integer data) {
        return data;
    }
@com.raizlabs.android.dbflow.annotation.TypeConverter
public class LongConverter extends TypeConverter<Long, Long> {

    @Override
    public Long getDBValue(Long model) {
        return model;
    }

    @Override
    public Long getModelValue(Long data) {
        return data;
    }
}
@com.raizlabs.android.dbflow.annotation.TypeConverter
public class FloatConverter extends TypeConverter<Float, Float> {

    @Override
    public Float getDBValue(Float model) {
        return model;
    }

    @Override
    public Float getModelValue(Float data) {
        return data;
    }
}
@com.raizlabs.android.dbflow.annotation.TypeConverter
public class DoubleConverter extends TypeConverter<Double, Double> {

    @Override
    public Double getDBValue(Double model) {
        return model;
    }

    @Override
    public Double getModelValue(Double data) {
        return data;
    }
}
0reactions
agrosnercommented, Jun 8, 2017

fixed in develop. should be in 4.0.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

ResultSet.getFloat return 0 instead of NULL - Stack Overflow
Yes. You can call ResultSet.wasNull() which reports whether the last column read had a value of SQL NULL . Note that you must...
Read more >
Keep default value as 0.0 for 0 or NULL for numeric column
I have a numeric column Number(28,3) in which I have to insert value as 0.0 , whenever I encounter 0 or NULL/blank at...
Read more >
Get 0 from database with data type is numeric - OutSystems
How can i get data 0 from database field column with data type numeric,. because in OutSystems entity with data type numeric 0...
Read more >
Database Engine events and errors - SQL Server
ls' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
Read more >
Inserting Null Into an Integer Column Using JDBC - Baeldung
It represents a special value. It's a common perception that null has no value or that it represents nothing. Having a null stored...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found