Roman
Roman

Reputation: 131278

Why an object is invisible?

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();
        }
    });
}

Upvotes: 0

Views: 270

Answers (4)

Jay
Jay

Reputation: 27512

Ditto Roman, but let me add one clarifying point.

If you do not have visibility to an object, you will get an error message when you try to compile. A null pointer exception is a RUNTIME error, not a compile-time error. You will never, ever get a null pointer exception for a visibility problem, for the simple reason that you will not be able to run the program if you have visibility problems, because the compile will have failed.

A null pointer exception means that you have not assigned an actual instance of the object to a variable. That could mean you never created an instance at all, or it could mean that you have one declaration hiding another, like here.

Upvotes: 1

Paulo Guedes
Paulo Guedes

Reputation: 7279

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.

Upvotes: 8

Anon
Anon

Reputation: 2674

You appear to be creating this new thread in an instance initializer. Which means that the containing instance may not be fully initialized when the thread starts running (you don't show where you call start()).


And some unasked-for advice: (1) don't subclass thread, create a Runnable, and (2) don't create your own threads, use an ExecutorService

Upvotes: 2

Woot4Moo
Woot4Moo

Reputation: 24336

Agent was never initialized. You want something like this:
Agent agent = new Agent();

Upvotes: 3

Related Questions