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 8931341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:05:47+00:00 2026-06-15T09:05:47+00:00

There are 3 classes. CallMe The basic class which prints the message Caller Implements

  • 0

There are 3 classes.

  1. CallMe The basic class which prints the message
  2. Caller Implements Runnable and it gets the object from the main class Synch
  3. Synch The main class which creates the object for CallMe and passes the same to class Caller class and starts the thread.

Question:
In the Synch class, what is the need to pass the object to the class Caller? When I tried a call to the class Caller without the object of CallMe the compiler throws a NullPointerException. Could you please provide any reasons for this behaviour.

eg : Caller ob1 = new Caller("Hello"); // calling without an object of class "CallMe"

Below is the working code for reference.

public class CallMe {
    void call(String msg) {
        System.out.print("[" + msg);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
        System.out.println("]");
    }
}

class Caller implements Runnable {
    String msg;
    CallMe target;
    Thread t;

    public Caller(CallMe targ, String s) {
        target = targ;
        msg = s;
        t = new Thread(this);
        t.start();
    }

    public void run() {
        target.call(msg);
    }
}

class Synch {
    public static void main(String args[]) {
        CallMe target = new CallMe();
        Caller ob1 = new Caller(target, "Hello");
        Caller ob2 = new Caller(target, "Synchronized");
        Caller ob3 = new Caller(target, "World");
        // wait for threads to end
        try {
            ob1.t.join();
            ob2.t.join();
            ob3.t.join();
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
    }
}
  • 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-15T09:05:48+00:00Added an answer on June 15, 2026 at 9:05 am

    The compiler throws a NullPointerException because of the line

    target.call(msg)
    

    If you pass null to the Caller constructor, then the instance variable target gets set to null, and you’re calling a method on a null reference, hence NullPointerException.

    As for why you need to have both a ‘Caller’ and a ‘CallMe’ class, it’s just a matter of style. The idea is to create a Runnable ‘wrapper class’ for whatever business logic your code may include. That way threading is not tied into the core of your project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two classes: A and B . There are algorithms for converting from
There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
Are there any other classes provided by php5+ such as the mysqli() class? for
In the Code Below there are Two Classes. One Object of type two is
Are there any classes/frameworks within the iOS SDK which can be used to easily
Are there any classes that don't inherit Object as SuperClass or maybe have become
in my project there are classes containing @Transactional(rollbackfor=Exception.class) . When compiling and packaging my
I am building an education related app. There are classes (as in a class
I have two controller class named TEnterCorrectCodeController and TEmergencyCancelController. In Both the classes there
I have there domain classes: Person. (Person.ID, Name,Address) Designation.(Designation.ID, Title, Band) SalarySlip (Person.ID, Designation.ID,

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.