0

I have a class Food which creates foods and starts a new thread. Amongst other things.

private Thread foodThread;
public void start() {
    foodThread = new Thread(this, "Food");       
    foodThread.start();
}

The I have a board class which has an Arraylist buffet which uses the methods from Food.

ArrayList<Food> buffet;
for (int i=0; i<15; i++){
        buffet.add(new Food(this));

    }

Now what I want to do is start a new thread from the Food class so (foodThread) inside the for loop. I tried

public Food food; 

and then

food.start();

inside the loop but that doesn't seem to work, it just throws an error.java.lang.NullPointerException

2
  • Please post enough code to reproduce the issue and (or at least) a stacktrace. Commented Nov 13, 2014 at 13:17
  • Are you sure you want a thread for each Food? Threads are heavyweight and complicated things, and the complexity of using them correctly needs to be offset by the benefits of concurrent execution for the particular application. I can't imagine what a "Food" object could be doing that requires that level of concurrency, unless perhaps it's simulating biological decay at the molecular level. But if you're using threads just to get practice with them, fair enough. Commented Nov 13, 2014 at 17:12

2 Answers 2

2

If you have this in your code :

public Food food; // <- Here you only declare a variable "food" of Type "Food" 
                  //    No object (of Type Food) instance has been created, yet!
food.start();     // <- Will throw NPE

Like this, then you have to also instantiate a Food object!

Like ...

public Food food = new Food(); 
food.start();

Another possible source of the NPE is in @subash 's answer ...

Just a hint for future questions: In the stacktrace ( the error messages that are displayed, where you saw the NullPointerException) there should be even line numbers and packages/classes mentioned. So you should especially in the case of NPE be able to track down where you forgot to instantiate an object.

UPDATE

If you changed

for (int i=0; i<15; i++){
    buffet.add(new Food(this));
}

to this:

for (int i=0; i<15; i++){
    Food localFood = new Food(this); // Create instance, variable is only valid and visible inside this block.
    buffet.add(localFood);           // Add the instance to your buffet.
    localFood.start();               // Start the instance's thread.
}                                    // Next, please.

it should do what you intended.

Sidenote: you should code against Interfaces. That means you better do something like

List<Food> buffet = new ArrayList<Food>();
// ^-Interface "List"    ^- Concrete implementation of the interface. Easily exchangeable for other implementation later on.
Sign up to request clarification or add additional context in comments.

4 Comments

Is there any other way to call a thread from one class inside another without public Food food = new Food(); food.start();
@user3470812 What do you mean exactly with "call a thread". There are many ways to create and start Threads. Extend Thread, implement Runnable, use Executors ...
Start the thread foodThread inside the Board class without public Food food;
@user3470812 see my update. The Food-instance does not need to be global.
1

initialize the ArrayList

ArrayList<Food> buffet = new ArrayList<Food>();

Comments

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.