2 Replies Latest reply on Jan 18, 2004 5:01 PM by 807595

    NullPointerException error

    807595
      Hello, I am trying to write a simple GUI using awt but keep getting a NullPointerException error:

      java.lang.NullPointerException
      at MazeGUI.actionPerformed(MazeGUI.java:82)
      etc...

      Line 82 is the following statement in the code:
      if (!newMaze.isWall(2,2))

      I assume that my Maze object (newMaze) is not initialized properly, but I'm not sure why. Any help much appreciated!

      The code...:
      import java.awt.*; 
      import java.awt.event.*; 
      class MazeGUI extends FrameWithClose implements WindowListener, ActionListener
      {
      private Panel fileNamePanel = new Panel();
      private Panel mazePanel = new Panel();
      private Panel controlPanel = new Panel();
      private TextField fileNameText = new TextField(30);
      private TextArea mazeArea = new TextArea(20,20);
      private String fileName = new String();
      private String constructFileName = new String();
      private Maze newMaze;
      private PlayerRobot pRobot;
      private HunterRobot hRobot;
      private boolean mazeLoaded = false;
      
      public MazeGUI(String title)
      {
      super(title);
      setLayout(new BorderLayout());
      
      fileNamePanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
      mazePanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
      controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
      
      Button importFileButton = new Button("Import");
      Label fileNameLabel = new Label("Enter Filename & Path here...");
      //TextField fileNameText = new TextField(30);
      //TextArea mazeArea = new TextArea("Maze Goes Here",20,20);
      Button upButton = new Button("Up");
      //upButton.setSize(10,20);
      Button rightButton = new Button("Right");
      
      fileNamePanel.add(fileNameLabel);
      fileNamePanel.add(fileNameText);
      fileNamePanel.add(importFileButton);
      mazePanel.add(mazeArea);
      controlPanel.add(upButton);
      controlPanel.add(rightButton);
      
      add(fileNamePanel,"North");
      add(mazePanel,"Center");
      add(controlPanel,"South");
      
      importFileButton.addActionListener(this);
      upButton.addActionListener(this);
      rightButton.addActionListener(this);
      
      Maze newMaze = new Maze();
      pRobot = new PlayerRobot();
      }
      
      public void actionPerformed(ActionEvent e)
      {
      
      String buttonLabel = e.getActionCommand();
      
      if (e.getSource() instanceof Button)
      
      if (buttonLabel.equals("Import"))
      {
      
      this.initializeMaze();
      
      
      }
      if (buttonLabel.equals("Up"))
      {
      
      
      if (mazeLoaded)
      {
      if (!newMaze.isWall(2,2))
      {
      newMaze.updateMaze(pRobot.getRow()-1,pRobot.getColumn(),'R');
      pRobot.moveSquare(pRobot.getRow()-1,pRobot.getColumn());
      mazeArea.setText(newMaze.getMaze());
      }
      else
      {
      mazePanel.setBackground(Color.cyan);
      repaint();
      }
      }
      else
      {
      mazePanel.setBackground(Color.yellow);
      repaint();
      }
      }
      }
      
      
      public void initializeMaze()
      {
      
      String fileName = fileNameText.getText();
      Maze newMaze = new Maze(fileName);
      mazeLoaded = true;
      
      newMaze.updateMaze(newMaze.getStartRow(),0,'R');
      newMaze.updateMaze(newMaze.getHeight()-2,newMaze.getWidth()-2,'H');
      mazeArea.setText(newMaze.getMaze());
      PlayerRobot pRobot = new PlayerRobot(newMaze,'R');
      
      pRobot.setRow(newMaze.getStartRow());
      pRobot.setColumn(0);
      
      
      }
      
      }