Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Norm

Thread: I need help with my game

  1. #1
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default I need help with my game

    I'm trying to get my GameMap class to update the map icons when the user enters text into the TextField component in GamePanel class. When the user enters text the GamePanel calls the updateMap() method in GameMap to be updated.

    Errors: Null Pointer Exception in the actionPerformed() method in GamePanel

    GamePanel
    Java Code:
    public class GamePanel extends JPanel implements ActionListener
    {
        private JTextField textField;
        private JTextArea textArea;
        private String longLocation;
        private String shortLocation;
        private Game game;
        private GameMap map;
    
        public GamePanel() 
        {
           super(new GridBagLayout());
           
           game = new Game();
           textField = new JTextField(20);
           textField.addActionListener(this);     
           
           textArea = new JTextArea(2, 10);
           textArea.setText(game.printWelcome());         
           
           textArea.setEditable(false);
           JScrollPane scrollPane = new JScrollPane(textArea);
           GridBagConstraints c = new GridBagConstraints();
           c.gridwidth = GridBagConstraints.REMAINDER;
           c.fill = GridBagConstraints.HORIZONTAL;
           add(textField, c);
           c.fill = GridBagConstraints.BOTH;
           c.weightx = 1.0;
           c.weighty = 1.0;
           add(scrollPane, c);
        }
    
         public void actionPerformed(ActionEvent evt) 
         {
            
            String text = textField.getText();    
            
            textArea.append(text + "\n");
            
            longLocation = game.go(text);
            shortLocation = game.getShortLocation();
            
            textArea.append(longLocation + "\n");
            
            map.updateMap(shortLocation);
            textField.selectAll();
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
         
        public void main(String[] args) 
        {
           new GameFrame();
        }
    
    }
    GameMap
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GameMap extends JPanel
    {
        JLabel label1, label2, label3, label4;
        Icon icon = new ImageIcon("test1.gif");
        Icon icon2 = new ImageIcon("test2.gif");
        Game game;
    
        public GameMap()
        {
          super(new GridLayout(5,1,5,5));//last 2 arguments = hgap, vgap  
    
          label1 = new JLabel("",icon, JLabel.CENTER);
          label2 = new JLabel("",icon, JLabel.CENTER);
          label3 = new JLabel("",icon, JLabel.CENTER);
          label4 = new JLabel("",icon, JLabel.CENTER);
          
          this.add(label1);
          this.add(label2);
          this.add(label3);
          this.add(label4);
        }
    
    public void updateMap(String shortLocation) 
    {   
         System.out.println("\n\n\n\n" + shortLocation);   
          if(shortLocation.equals("the start"))
          {
              label4.setIcon(icon2);
              label4.setText("START");
          }
           if(shortLocation.equals("corridor1"))
          {
             label3.setIcon(icon2);
             label3.setText("corridor1");
          }
           if(shortLocation.equals("room2"))
          {
             label2.setIcon(icon2);
             label2.setText("room2"); 
          }
           if(shortLocation.equals("room3"))
          {
             label1.setIcon(icon2);
             label1.setText("room3"); 
          } 
    
          repaint();
    }
    }
    GameFrame - adds the two JPanels to the JFrame
    Java Code:
    public class GameFrame extends JFrame
    {
        private String locationToPanelShort = "the start";
        GameMap map;
        GamePanel panel;
        
        public GameFrame() 
        {
            super("Game Example");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(800, 600);
    
            GamePanel panel = new GamePanel();
            GameMap map = new GameMap();
            
            add(panel, BorderLayout.NORTH);
            add(map, BorderLayout.SOUTH);
            
            panel.setPreferredSize(new Dimension(800,300));
            setVisible(true);
        }
          
        public static void main(String[] args)
        {
            new GameFrame();      
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I need help with my game

    Errors: Null Pointer Exception in the actionPerformed() method in GamePanel
    Post the stack trace and indicate which line of code is throwing the NPE.

    db

  3. #3
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at GamePanel.actionPerformed(GamePanel.java:58)
    	at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
    	at javax.swing.JTextField.postActionEvent(JTextField.java:705)
    	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
    	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1639)
    	at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
    	at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
    	at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
    	at java.awt.Component.processEvent(Component.java:6066)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4652)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4482)
    	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
    	at java.awt.Component.dispatchEventImpl(Component.java:4524)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4482)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:603)
    	at java.awt.EventQueue$1.run(EventQueue.java:601)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:617)
    	at java.awt.EventQueue$2.run(EventQueue.java:615)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I need help with my game

    Quote Originally Posted by chara View Post
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at GamePanel.actionPerformed(GamePanel.java:58)
    So, would you like to tell us which is line 58 of GamePanel.java or are we expected to count the lines (without really knowing if the code that threw the exception was exactly as posted)?

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I need help with my game

    Quote Originally Posted by DarrylBurke View Post
    (without really knowing if the code that threw the exception was exactly as posted)
    Which would have to be the case, as the GamePanel code posted doesn't even have 58 lines.

    db

  6. #6
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    Sorry, i forgot. Line 58 in GamePanel is

    map.updateMap(shortLocation);

    It sends the new location to the GameMap class so it can update to a new map.

    If i create a new instance of GameMap. map = new GameMap();

    The errors go away and there are no other errors, but it doesn't work properly as it should be updating the current Map on the frame.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: I need help with my game

    You need to have access to the map variable in the listener method that has the value of the current Map.
    Your code may be hiding/shadowing the map variable. Look in the constructor and the class level variables.

  8. #8
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    Quote Originally Posted by Norm View Post
    You need to have access to the map variable in the listener method that has the value of the current Map.
    Your code may be hiding/shadowing the map variable. Look in the constructor and the class level variables.
    In GamePanel i have declared the following variables:

    private JTextField textField;
    private JTextArea textArea;
    private String longLocation;
    private String shortLocation;
    Game game;
    GameMap map;


    How do i know if the code is hiding the map variable?
    Also, if i make a new instance of GameMap() the error goes away, but it doesn't do what its suppose to do.

    Go game structure goes like this:
    GamePanel and GameMap has access to Game class, its methods, and its associated classes (Player, room, command) methods.

    GameFrame is a JFrame that adds the 2 JPanels (GamePanel, and GameMap) on the frame, and has no inheritance of Game.
    Last edited by chara; 09-18-2011 at 02:06 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: I need help with my game

    How do i know if the code is hiding the map variable?
    Look in your code for statements like this: GameMap map that define a variable.
    There should only be ONE definition for the variable. If there are two definitions, then one is hiding the other.
    chara likes this.

  10. #10
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    GameFrame has the variable GameMap map, but if i omit it, how would i add the GameMap(JPanel) on the GameFrame (JFrame).


    Would i need to add the GameMap() in the GamePanel class. If so, how would i do this?

    I tried the following in the GamePanel


    GameMap map = new GameMap();
    this.add(map,BorderLayout.SOUTH);

    to only find i get an error saying "java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint at java.awt.GridBagLayout.addLayoutComponent(GridBagL ayout.java:685)" when i go to run the game

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: I need help with my game

    What layout manager does the container have?

  12. #12
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    EDIT: I FIGURED IT OUT. Thank you very much this has been bugging me for days......

    I found out that the simplest solution is the easiest way - i eliminated the GameMap and GameFrame class and added to the one class.
    Last edited by chara; 09-18-2011 at 02:50 PM.

  13. #13
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    One small problem... how do i set a the gridLayout outside of a static void createAndShowGUI() because if i try declaring the variables outside it says "non-static variable cannot be referenced from a static context".





    So i have to declare the labels like so:
    Java Code:
    public GamePanel()
    {
        //stuff 
         addIcons();
    }
      private static void createAndShowGUI()
        {
            JFrame frame = new JFrame("TextDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new GamePanel());
            
            //create a gridlayout to put the icons onto it -- this doesnt work as i cannot reference it outside of this method
            JPanel map = new JPanel(new GridLayout(5, 6, 1, 1));
            frame.add(map, BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);
        }
      public void addIcons() //how do i make use of the map JPanel in this method?
        {  
            label1 = new JLabel("",icon, JLabel.CENTER);
            label2 = new JLabel("",icon, JLabel.CENTER);
            label3 = new JLabel("",icon, JLabel.CENTER);
            this.add(label1);                           
            this.add(label2);
            this.add(label3);
       }
     public static void main(String[] args)
     {
                public void run() {
                    createAndShowGUI();
                }
           });
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: I need help with my game

    Move the code into the constructor.

  15. #15
    chara is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: I need help with my game

    Quote Originally Posted by Norm View Post
    Move the code into the constructor.
    What part of the code? Do you mean the

    JPanel map = new JPanel(new GridLayout(5, 6, 1, 1));


    If i do this, i can't add it to the frame which is created in the static void createAndShowGUI()

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default Re: I need help with my game

    non-static variable cannot be referenced from a static context".
    The part that gives this error.

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  2. Tic Tac Toe Game
    By hoangx87 in forum New To Java
    Replies: 10
    Last Post: 04-18-2011, 09:23 AM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •