-1

Hey guys I'm a beginner (sort off) in java and i'm having some trouble. I'm trying to get the date I set in the Budget class with a variable in the Finances class but I keep getting a NullPointerException.

I'm trying to use the dueDate variable in the finances class but the logCat keeps stating that it's a null object reference even after I've instantiated it.

public class Budget {
    private Date mDate = new Date();
    public static double budgetAmount;

    private static Budget instance = new Budget();
    private Budget(){

    }

    public double getBudget(){
        return budgetAmount;
    }

    public void setBudget(double budget){
        budgetAmount = budget;
    }

    public static Budget getInstance(){
        return instance;
    }


    public Date getDate() {
        return mDate;
    }

    public void setDate(Date date) {
        mDate = date;
    }

}

Here is the Financial Class

public class Finances extends Activity{
    int amtFood; int amtCar; int amtGas; int amtTvBill; int amtInBill;
    int amtTronics; int amtWear; int amtLight; int amtWater; int amtOthers;

    Date nowDate;
    Date dueDate = new Date();

    double MainAmount = Budget.budgetAmount;
    static int SpentAmount;

    Budget budgetDate;

    public Finances(){
        HashMap<String,Integer> amtAdd = ExpenditureLab.get(this).getExpendMap();
        int food= amtAdd.get("Groceries/Food");
        int car = amtAdd.get("Car servicing");
        int gas = amtAdd.get("Gas Money");
        int tvBill = amtAdd.get("Television Bill");
        int inBill = amtAdd.get("Internet Bill");
        int tronics = amtAdd.get("Electronics");
        int wear = amtAdd.get("Clothes/Wearables");
        int light = amtAdd.get("Light Bill");
        int water = amtAdd.get("Water Bill");
        int others = amtAdd.get("Others");



        dueDate = budgetDate.getDate();
    }

    public void addSpent(int amt){
        SpentAmount += amt;
    }

}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
user3332929
  • 175
  • 1
  • 1
  • 6

4 Answers4

1

budgetDate is never initailized.

Budget budgetDate; #=> this is null

and then you try call getDate() on the null object.

dueDate = budgetDate.getDate(); #=> causing your nullPointerException?

Therefore you need to instantiate your budgetDate object first.

Budget budgetDate = new Budget();

and then set the mDate object

budgetDate.setDate(someDate); #=> someDate is a Date object

before calling

dueDate = budgetDate.getDate();
Danoram
  • 8,132
  • 12
  • 51
  • 71
1

Yes budgetDate is not initialized, leading to null pointer exception. Also do not use constructor in an Activity, you can have all your initializations in the onCreate method of your Activity.

Chamu
  • 194
  • 5
0

dueDate isn't the null object, you're setting it to budgetDate.getDate(), and budgetDate has not been instantiated yet.

Rich
  • 36,270
  • 31
  • 115
  • 154
0

I don't see the place you assign value to budgetDate (is never initailized). It's null pointer exception when you call dueDate = budgetDate.getDate();

Solution: Place budgetDate = new Budget() before dueDate = budgetDate.getDate(); like that:

@Override
public void onCreate(Bundle bundle) {
    HashMap<String,Integer> amtAdd = ExpenditureLab.get(this).getExpendMap();
    int food= amtAdd.get("Groceries/Food");
    int car = amtAdd.get("Car servicing");
    int gas = amtAdd.get("Gas Money");
    int tvBill = amtAdd.get("Television Bill");
    int inBill = amtAdd.get("Internet Bill");
    int tronics = amtAdd.get("Electronics");
    int wear = amtAdd.get("Clothes/Wearables");
    int light = amtAdd.get("Light Bill");
    int water = amtAdd.get("Water Bill");
    int others = amtAdd.get("Others");

    budgetDate = new Budget();
    dueDate = budgetDate.getDate();
} 
Trần Đức Tâm
  • 4,037
  • 3
  • 30
  • 58