实体构造函数给出"incompatible types"错误



因此,为了提供一点上下文,我目前正在开发一个带有数据库的应用程序。通过研究,很明显谷歌的Room(使用SQLite(是可行的。因此,我已经用外键和@Transaction(代表一对多或一对一关系(完全建立了数据库。

目前,我已经被一些事情困扰了一整天:每当我试图为实体(表(创建构造函数时,如果有字符串以外的参数,就会出现incompatible types: <null> cannot be converted to int错误。(或浮动或双重等(。

我只是不明白为什么会发生这种情况,我也尝试删除所有外键,并尝试使用尽可能少的代码,但它就是不起作用。

仅供参考:错误发生在生成的类中。这个类是从DAO接口(DatabaseAccessInterface(生成的,在这里可以列出所有查询函数,包括关系。生成的类调用相关实体的构造函数:像这样的_tmpFan = new Fan(null,null,null,null,null,null,_tmpCreatedAt);,比说incompatible types: <null> cannot be converted to int

以前有人遇到过这个错误吗?还是你必须坚持字符串?如果你需要知道我代码的一部分,只需询问,我就会提供它

接口内交易/关系:

@Transaction
@Query("SELECT * FROM measurement_session WHERE fan_id = :fanID")
public List<FansWithMeasureSessions> getMeasureSessionsOfFan(int fanID);

风扇类别:

@Entity (tableName = "fan",
foreignKeys = {
@ForeignKey(entity = MeasurementSession.class,
parentColumns = "fan_id",
childColumns = "fan_id",
onDelete = ForeignKey.CASCADE)
},
indices = {
@Index(value = "customer_id", unique = true)
}
)
public class Fan {
@ColumnInfo(name = "customer_id")
public int customerID;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "fan_id")
public int fanID;
@ColumnInfo(name = "fan_name")
public String fanName;
@ColumnInfo(name = "fan_type")
public String fanType;
@ColumnInfo(name = "diameter")
public float diameter;
@ColumnInfo(name = "length")
public float length;
@ColumnInfo(name = "width")
public float width;
@ColumnInfo(name = "created_at")
@TypeConverters(DateConverter.class)
public Date createdAt;
public Fan(int customerID, String fanName, String fanType, float diameter, float length, float width, Date createdAt) {
this.customerID = customerID;
this.fanName = fanName;
this.fanType = fanType;
this.diameter = diameter;
this.length = length;
this.width = width;
this.createdAt = createdAt;
}
}

FansWithMeasureSessions类:

public class FansWithMeasureSessions {
@Embedded public Fan fan;
@Relation(
entity = MeasurementSession.class,
parentColumn = "fan_id",
entityColumn = "fan_id"
)
public List<MeasurementSession> measurementSessions;
}

注释掉了我的大部分其他代码,同时仍然保持错误,所以应该这样做!

解决方案:

floatintdouble等使用可为null的类型。
这意味着您应该使用FloatIntegerDouble数据类型(可为null(

相关内容

最新更新