I have two classes
in class ABC
double a = 0.5
public double lala()
{
return a;
}
I want use it in another class, let us say class DEF
ABC abc;
double baba = abc.lala();
But it says that java.lang.NullPointerException, any idea? Thx
public class AdapterDB
{
double cal;
double rcarbohydrate;
double rfat;
double rprotein;
public AdapterDB(double cal, double rcarbohydrate, double rfat, double rprotein, Context ctx)
{
this.cal = cal;
this.rcarbohydrate = rcarbohydrate;
this.rfat = rfat;
this.rprotein = rprotein;
this.context = ctx;
DBHelper = new DatabaseHelper (context);
}
public double Calorie()
{
return cal;
}
public double Carbohydrate()
{
return rcarbohydrate;
}
public double Protein()
{
return rprotein;
}
public double Fat()
{
return rfat;
}
}
Here is my code for class ABC, and I want to use some of them at another class >_<
You need to allocate ABC in memory, otherwise it points to
null, hence NullPointerException:Edit: Ok, in case ABC does not have a zero-argument constructor like:
you have two options to solve your problem. Either add one and inside give default values to your other fields. Like:
Or, modify the creation of the object like: