Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:27:39+00:00 2026-06-09T22:27:39+00:00

So I’m doing this awkward example to understand how threading in Java works. It’s

  • 0

So I’m doing this awkward example to understand how threading in Java works.
It’s quite simple actually, however, I can’t seem to get why this gives me a NullPointerException exception when it tries to fire more than 3 threads.

Can you sort it out? Eclipse’s debugger doesn’t help 🙁

Thanks in advance!!

public class Main {

    public static void main(String[] args) {

        Main boot = new Main();
    }

    public Main()
    {
        CoolThread myThread = new CoolThread(1, 2);
        Thread t_myThread = new Thread(myThread);
        t_myThread.start();

        myThread.defineMain(this);
    }


    public void teste(String tests){
        CoolThread myThread = new CoolThread(1, 2);
        Thread t_myThread = new Thread(myThread);
        t_myThread.start();
    }

}

public class CoolThread extends Thread {

int firstNum;
int secondNum;
Main myMain;

/**
 * Constructor
 * @param firstNum
 * @param secondNum
 */
public CoolThread(int firstNum, int secondNum)
{
    this.firstNum = firstNum;
    this.secondNum = secondNum;
}

public void defineMain(Main myMain)
{
    this.myMain = myMain;
}
/**
 * Fires the thread.
 */
public void run()
{
    try{
        int number = 0;
        for(;;)
        {

            int soma = (firstNum+secondNum);
            System.out.println("ID: " +Thread.currentThread().getId());
            firstNum++;
            secondNum++;
            number++;
            Thread.sleep(100);
            if((number % 10) == 0)
            {
                myMain.teste("The sum is: " + soma);
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

}

By the way, this is the output I get:

ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 12
ID: 9
ID: 12
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 9
ID: 12
ID: 9
ID: 14
java.lang.NullPointerException
    at my.own.package.CoolThread.run(CoolThread.java:44)
    at java.lang.Thread.run(Thread.java:722)

And it goes on creating and killing threads…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:27:40+00:00Added an answer on June 9, 2026 at 10:27 pm

    You are either calling myThread.defineMain(...) after starting your thread (in Main) or not calling defineMain(...) at all (in teste(...)). You need to define main before the thread is running otherwise there is a chance that myMain is null when you get to line 44 which I assume is:

    myMain.teste("The sum is: " + soma);
    

    This is the definition of a thread race condition. Your start code should be:

    CoolThread myThread = new CoolThread(1, 2);
    // this must be done _before_ the thread starts below
    myThread.defineMain(this);
    Thread t_myThread = new Thread(myThread);
    t_myThread.start();
    

    Never think that the JDK is wrong. That will just kill any debugging and critical thinking that you are using to find your problem. Learn how to use the debugger in eclipse. You can then put a break-point on line 44 and investigate the variables.

    Unfortunately in this case you have a threaded program and the debugging is changing the timing of the program and most likely hiding the bug. You might have tried to print out the various objects on line 44 to see which one was null.


    Also, as @kurtzbot pointed out, if CoolThread extends Thread then you can just say new CoolThread() and then coolThread.start(). Really what you should be doing is having CoolThread implement Runnable instead of extending Thread. That’s the better pattern:

    CoolThread myThread = new CoolThread(1, 2);
    // this must be done _before_ the thread starts below
    myThread.defineMain(this);
    Thread t_myThread = new Thread(myThread);
    t_myThread.start();
    ...
    
    public class CoolThread implements Runnable {
        public void run() {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.