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)