wood burning stoves*
The moose likes Beginning Java and the fly likes Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Composition" Watch "Composition" New topic
Author

Composition

Thana Bujakorn
Greenhorn

Joined: Dec 18, 2002
Posts: 12
I have a problem with the following code:
//***Applet Class***
...
//declaration (buildingCanvas extends JPanel)
protected BuildingCanvas buildingCanvas;
protected SimManager manager;
//init()
...
BuildingCanvas buildingCanvas = new BuildingCanvas();
SimManager manager = new SimManager(buildingCanvas);
...
//my action method is as follow
public void actionPerformed(ActionEvent e){

if(e.getSource() == floor1Button)
manager.createPerson();
}
//***class SimManager***
//declaration
Person person; // class
BuildingCanvas displayPanel;
//Constructor
public SimManager(){
displayPanel = mainPanel;
}
public void createPerson(){

person = new Person();
displayPanel.addPerson(person);
}
The problem is: I get a NullPointer exception thrown everytime I try to call manager.createPerson();by clicking the button in the Applet class. The problem can be solved by declaring both variables(Person and BuildingCanvas) of SimManager static, what I wonder is, are there any better ways to solve the problem beside declaring everything static(cause there are many more of similar variables that need to be accessed in similar manner? Please help. and forgive me for my poor english.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Could you please provide the whole exception stack trace?
W.
Thana Bujakorn
Greenhorn

Joined: Dec 18, 2002
Posts: 12
Thanks so much for the reply, and in case my question is ambiguity, here is what I try to ask, how could I avoid using staic keyword for People object and createPerson() method, are there any other techniques? The reason is this, if I want to, says, add more method(s) to the SimManager class and the method deal with person object, for example isplayPanel.addPerson(person); the compiler error : non-static variable displayPanel cannot be referenced from a static context
displayPanel.addPerson(person); occurs everytime, which is again can be overcome by static keyword. I read from the book (could you please tell me if my understanding is right about this) that using static keword should be avoid as it violates a good object oriented programming practice.
I also miss typed the constructor for SimManager class. The correct two classes are:
//****************************************
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Simulator extends JApplet{
protected JButton floor1Button, floor2Button;
protected JPanel buttonPanel;
protected JTextArea ta;
protected BuildingCanvas buildingCanvas;
protected SimManager manager;
protected BorderLayout mainLayout;
ButtonHandler handler;

public void init(){

mainLayout = new BorderLayout();
Box b = Box.createHorizontalBox();
ta = new JTextArea(3,45);
ta.setLineWrap(true);
b.add(new JScrollPane(ta));

buttonPanel = new JPanel();
floor1Button = new JButton("Floor1");
floor2Button = new JButton("Floor2");
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(floor1Button);
buttonPanel.add(floor2Button);

buildingCanvas = new BuildingCanvas();
SimManager manager = new SimManager(buildingCanvas);

getContentPane().add(b, BorderLayout.NORTH);
getContentPane().add(buildingCanvas, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);

ButtonHandler handler = new ButtonHandler();
floor1Button.addActionListener(handler);
floor2Button.addActionListener(handler);
}

public void paint(){

}
private class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e){

if(e.getSource() == floor1Button)
manager.createPerson();
}
}
}
//****************************************
public class SimManager extends Object{

Person person;
BuildingCanvas displayPanel;

public SimManager(BuildingCanvas mainPanel){

displayPanel = mainPanel;
}

public void createPerson(){
Person person = new Person();
}

}
//**********************************
This is what happend when not declaring People object static:
C:\Documents and Settings\Thana\Desktop\Elevator>appletviewer Simulator.html
created
Exception occurred during event dispatching:
java.lang.NullPointerException
at Simulator$ButtonHandler.actionPerformed(Simulator.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
In the init method, you have a line:
SimManager manager = new SimManager(buildingCanvas);
It means that the SimManager manager you are instantiating is local to the method init. You have not instantiated your class attribute wearing the same name 'manager'.
Drop 'SimManager' in the init method:
manager = new SimManager(buildingCanvas);
W.
Thana Bujakorn
Greenhorn

Joined: Dec 18, 2002
Posts: 12
I took a whole day try to fig it out! herrrr, how dump? Thanks a lot.
Benjoe Reyes
Ranch Hand

Joined: Apr 24, 2002
Posts: 109
seems like your doing the elevator simulator by D&D... you look like you're done with the first version...
if you do, can you pm me? i'm having trouble with the class interaction
[ December 19, 2002: Message edited by: Benjoe Reyes ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Composition
 
Similar Threads
Focus Events
Componet Allignment
Package confusion
FlowLayout Display Problems
Writing my first program - need a little help!