Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    b is the veriable for Board. so i do that, then add(b); but, i get a long error when trying to run it, the frame still comes up, but no JPanle, and a bunch of errors in netbeans.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    i get a long error when trying to run it
    Please copy and paste here the full text of the error message. It contains info on what is causing the error.

  3. #23
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    This is the Error code.


    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at game.Board.paintComponent(Board.java:35)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
            at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
            at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
            at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
            at javax.swing.JComponent.paint(JComponent.java:1015)
            at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
            at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
            at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
            at java.awt.Container.paint(Container.java:1780)
            at java.awt.Window.paint(Window.java:3375)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            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)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at game.Board.paintComponent(Board.java:35)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
            at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
            at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
            at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
            at javax.swing.JComponent.paint(JComponent.java:1015)
            at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
            at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
            at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
            at java.awt.Container.paint(Container.java:1780)
            at java.awt.Window.paint(Window.java:3375)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            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. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at game.Board.paintComponent(Board.java:35)
    What variable is null at line 35 in Board class?
    Why is that variable null?
    Where do you give it a value?
    Does it have a value before you try to use it?

    You may need to do some debugging by adding some print outs in your program to see in what order different parts of your program are being executed. For example add one in the paintComponent method and add one where you give a value to the variable that is null in the paintComponent method.

  5. #25
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Ahh, idk! the only System.out.println("Check"); that works is if i put it in the main object int the main or frameSetup meathods. anywhere else i get the error message after with now out print! here our my three objects, can you get it to work? (you wll need to change the image in player.)

    main:
    Java Code:
    package game;
    
    import javax.swing.*;
    
    public class Main extends JFrame {
    //private Board b;
        public void frameSetup() {
           Board b = new Board();
            add(b);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setLocationRelativeTo(null);
            setResizable(false);
            setTitle("Jake's Game");
            System.out.println("Check");
            setVisible(true);
    
    
    
    
    
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
    
    
    
        }
    }
    Board:
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class Board extends JPanel implements ActionListener {
    
    
        private Timer timer;
        private Player play;
        
        public void gameSetup() {
    
            System.out.println("Check");
           
           addKeyListener(new aAdapter());
    
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
    
            play = new Player();
    
            timer = new Timer(5, this);
            timer.start();
    
        }
    
        
        @Override
         public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(play.getImage(), play.getX(), play.getY(), this);
    
            System.out.println("Paint");
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
        
    
        public void actionPerformed(ActionEvent e) {
    
            play.move();
            repaint();
        }
    
        private class aAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                play.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                play.keyPressed(e);
            }
        }
    }
    Player:
    Java Code:
    package game;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Player {
    
        private String play = "sci.jpg";
        private int vx;
        private int vy;
        private int x;
        private int y;
        public Image image;
    
        public Player(){
            ImageIcon ii = new ImageIcon(this.getClass().getResource(play));
            image = ii.getImage();
    
            x = 40;
            y = 60;
        }
    
        public void move(){
            x += vx;
            y += vy;
        }
    
        public int getX(){
            return x;
        }
        public int getY(){
            return y;
        }
        public Image getImage(){
            return image;
        }
    
        public void keyPressed(KeyEvent e){
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                System.out.println("Left");
                vx = -1;
               
            }
            if(key == KeyEvent.VK_RIGHT){
                System.out.println("Right");
                vx = 1;
            }
            if(key == KeyEvent.VK_UP){
                System.out.println("Up");
                vy = -1;
            }
            if(key == KeyEvent.VK_DOWN){
                System.out.println("Down");
                vy = 1;
            }
        }
        public void keyReleased(KeyEvent e){
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                 
                vx = 0;
            }
            if(key == KeyEvent.VK_RIGHT){
                
                vx = 0;
            }
            if(key == KeyEvent.VK_UP){
               
                vy = 0;
            }
            if(key == KeyEvent.VK_DOWN){
              
                vy = 0;
            }
        }
    
    }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Do you know which variable is null and is causing the NPE?
    Did your print outs show when the variable has been given a value?
    Was the variable given a value before the error or after it?

    There is a timing problem in your code. You are trying to use a variable before it has been given a value.
    You need to track down why that is.

  7. #27
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    and, i have NO idea what veriable that is. wich is where the problem lies.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Do you have a program like an IDE or editor that will help you find line 35 in the Board class? You need to use that to find the line where the problem is. Or just use you finger to count the lines.

    If you can't find line 35, I don't know how anyone can help you.

  9. #29
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    well, the problem lays in This string of code:

    g2d.drawImage(play.getImage(), play.getX(), play.getY(), null);

    i can system out print something before this, but not after it. and its line 37: the error in board class, what have i called wrong here?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Where is the variable: play given a value? Obviously it is not until after the NPE.
    Why is paintComponent being called before the variable is given a value?

  11. #31
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Private player play;
    play, IS the veriable for Player, wich is my object. but, im haveing another problem as well, now, my gameSetup meathod is not being called because all i call is my board class.

  12. #32
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    ! i fixed it. thank you so much for the help. what i was missing, (idk why, i guess i was just haveing a dumb moment) was that i add board to jFrame, but i never called gameSetup(); and for somereason i didnt think to do so, but by doing so, i now have the squar moveing around the screen. thank you for all the help (:

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Huge problem. PacMan game.
    By Bullfrog in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 02:39 PM
  2. Need smart solution on java game lag problem..
    By Addez in forum New To Java
    Replies: 0
    Last Post: 11-13-2009, 12:48 PM
  3. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-05-2009, 11:18 PM
  4. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Tags for this Thread

Posting Permissions

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