0

My problem is exactly as the title states.

I have a Tracker.class, a TrackerGUI.class and a TrackerApp.class (driver for JFrame). Tracker class is initialised in the TrackerGUI.class as private Tracker tracker

The code itself compiles properly (so I'm guessing that means there are no syntax errors, but when I try to add an object, Terminal pops up (I'm using BlueJ), and gives me this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TrackerGUI$Food.actionPerformed(TrackerGUI.java:121)

clicking that would lead me to:

public class TrackerGUI extends JFrame {
    private Tracker tracker;
    double calories;
    double quantity;
    // more initialising of GUI elements; so all the JTextField things are initialised
    // setting up the GUI code, too

    public class Food implements ActionListener {
        public void actionPerformed (ActionEvent ae) {
            double calories = Double.parseDouble(caloriesField.getText().replaceAll(",",""));
            String newFood = activityField.getText();
            if(calories < 0) {
                textArea.append("Calories cannot be negative. \n\n"); 
            }
            else {
                tracker.addFood(newFood, calories); // this line!
                textArea.append(newFood + " with " + calories + " calories added to database! \n");
            }
        }
    }

In the tracker class, the code for addFood is such:

public class Tracker {
    ArrayList<Food> foodBase = new ArrayList<Food>();
    String foodName;
    Double calories;

public void addFood(String foodName, double calories)
{
    Food newFood = new Food(foodName, calories);
    foodBase.add(newFood);
}

What is missing in my code? Note: I have to use ArrayList and I can't use List or Map I feel like I'm missing a for statement that gets foodBase.size() but I'm not sure where it would fit?

3
  • 1
    Did you create new instance of Tracker in TrackerGUI? Commented Mar 1, 2014 at 13:28
  • which line is: TrackerGUI.java:121 ? Commented Mar 1, 2014 at 13:29
  • ok, I have seen you marked the line as comment! please check the updated answer ;-) Commented Mar 1, 2014 at 13:42

2 Answers 2

1
ArrayList<Food> foodBase = new ArrayList<Food>();

You're declaring and initializing a local variable in your constructor, which happens to have the same name as the field, used in the addFood() method. It should be

this.foodBase = new ArrayList<Food>();

EDIT: reading the exceptin again, it seems you also forgot to initialize the tracker in the class containing the Food inner class (TrackerGUI)

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting the 'illegal start of type' error when I change it. I have a separate food class, if that information helps.
Post the relevant code (i.e. the Tracker class, with its fields and constructor), the complete and exact error message, and tell us which line it refers to. Note that, reading the exceptin again, it seems you also forgot to initialize the tracker in the class containing the Food inner class (TrackerGUI).
the start of my public class TrackerGUI extends JFrame has private Tracker tracker;
And this tracker is null, until you initialize it.
0

change this:

    else {
        tracker.addFood(newFood, calories); // this line!
        textArea.append(newFood + " with " + calories + " calories added to database! \n");
    }

to:

    else if (tracker != null){
        tracker.addFood(newFood, calories); // this line!
        textArea.append(newFood + " with " + calories + " calories added to database! \n");
    }

to ensure you don't execute this code if tracker is null AND make sure before that you initialized the tracker properly before:

 private Tracker tracker =  new Tracker(); // initialize your tracker!!

4 Comments

The tracker should be initialized. The if block will only make the problem harder to diagnose if the OP forgets to initialize it. A NullPointerException is clearer to diagnose than a GUI which doesn't do anything, without any error message.
I think I've been looking at too many declarations that I completely forgot how to initialise. Thank you for pointing it out!
@user3364528 No problem! Did it work with this initialization in the end?
I haven't printed out the contents of the array yet, but since my else says "Code Error" (so that I know if I'm doing anything wrong), and the text I'm appending in my else if statement is appending to my textArea, I'm pretty sure it's working. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.