Jesbin MJ
Jesbin MJ

Reputation: 3249

passing null value as a parameter in java

i have a method to insert datas into an sq-lite db

 public long InsertDetails(int Id,String BatchName,double Weight,
        double Yield)
{
    ContentValues InsertContentValues = AssesmentInsertValues(Id,BatchName,Weight,Yield);
    return this.data.insert("tb_LabAssessment", null, InsertContentValues);
}

from my main class i m passing values as a parameter to this above function, in some cases i want to pass weight and yield as null values.

String batchname;
double weight;
double Yield;
dbAdapter.InsertAssesmentDetails(objClsProduct.getId(),batchname,weight,Yield)

how can i pass the values of weight and yield as null here.

Upvotes: 0

Views: 15941

Answers (4)

Kitesurfer
Kitesurfer

Reputation: 3561

Introduce second Method with parameter you need. Passing NULL as allowed need to be documented in some way. An alternative methode makes this obsolete and you're done.

Upvotes: 1

stinepike
stinepike

Reputation: 54672

primitive types like double can not be null. You can send Double object instead. In that case in the method signature use Double instead of double. Then call the method like following

dbAdapter.InsertAssesmentDetails(objClsProduct.getId(),batchname,null,null)

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117589

Change the method to:

public long InsertDetails(int Id, String BatchName, Double Weight, Double Yield)

where Double can hold null, since it's a reference type.

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93561

You can't pass a primitive as null. There's two ways to do this- first is to make the function take Double- the class version of a double. The other is to pass in some unused value, such as -1, to mean null. That requires there to be a reasonable default value though.

Upvotes: 6

Related Questions