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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:02:20+00:00 2026-05-18T10:02:20+00:00

I have a class. In this class I declare a a private variable private

  • 0

I have a class. In this class I declare a a private variable private Agent agent;. In my class I have

private Thread controller = new Thread() {
...
}

Within the above private thread I call getParameter which is a private method of the considered class. Within the getParameter I call one of the methods of the agent. As a result I get a NullPointerException. So, I conclude that agent is not visible from the getParameter.

Why is that? Can it be that the reason of that is that the getParameter is within the private Thread? And, if it is the case, how the described problem can be resolved?

ADDED

I realized that I need to be more specific. My code is organized like that:

public class GameWindow {
   ...
   private Agent agent;
   ...
   private Thread controller = new Thread() {
        public void run() {
           ...
           Agent agent = new Agent();
           ...
           parameter = getParameter();
        }
  }
  ...
  private String getParameter() {   
     ...
     agent.someMethod();
     ...
  }
}

ADDED 2
In the GameWindow I have:

public void startWindow() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            controller.start();
        }
    });
}
  • 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-05-18T10:02:20+00:00Added an answer on May 18, 2026 at 10:02 am

    The NullPointerException has nothing to do with visibility. You are probably calling the getParameter method of a null object. Your agent variable is declared, but not initialiazed. You may want to code something like:

    private Agent agent;
    private Thread controller = new Thread() {
        agent = new Agent();
        // ...
        agent.getParameter(); // should not throw a NullPointerException
    }
    

    UPDATE AFTER CODE WAS ADDED

    Your code has two definitions of agent. The first one in the class:

    private Agent agent;
    

    And the second one inside your run() method:

    Agent agent = new Agent();
    

    Your getParameter() method does not know the agent defined inside the run() method. It only knows the agent member of the class, which was not initialized. Your problem will be solved when you remove the second definition of agent:

    public void run() {
           ...
           // Agent agent = new Agent();
           agent = new Agent(); // works fine: you are initiliazing the class member
           ...
           parameter = getParameter();
     }
    

    OLD UPDATE:
    The NullPointerException is thrown inside the getParameter() method. So I understand your code is like the following:

    private Something getParameter() {
        agent.someMethod(); // exception thrown here
    }
    

    If this is your code, the problem remains the same: agent is not initialized. You must initialize it before calling any of its methods inside the getParameter() method.

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

Sidebar

Related Questions

I have this class for page titles: class UI { private static $title; public
Imagine you have a class with dozens of private member variables. Each member variable
I have a class like this public class foo { private void getThread() {
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;
I have a class like this: public class Stretcher : Panel { public static
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class in my domain model called JobPlan this class is stored
I have a class Application that my global.asax inherits from. The class has this

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.