Results 21 to 33 of 33
Thread: My java game problem!? Help!!!
- 09-25-2010, 06:07 PM #21
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
- 09-25-2010, 06:11 PM #22
Please copy and paste here the full text of the error message. It contains info on what is causing the error.i get a long error when trying to run it
- 09-25-2010, 06:13 PM #23
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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)
- 09-25-2010, 06:28 PM #24
What variable is null at line 35 in Board class?Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.Board.paintComponent(Board.java:35)
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.
- 09-25-2010, 06:37 PM #25
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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:
Board: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(); } }
Player: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); } } }
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; } } }
- 09-25-2010, 06:55 PM #26
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.
- 09-26-2010, 07:03 PM #27
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
and, i have NO idea what veriable that is. wich is where the problem lies.
- 09-26-2010, 07:16 PM #28
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.
- 09-26-2010, 08:31 PM #29
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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?
- 09-26-2010, 08:40 PM #30
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?
- 09-26-2010, 08:46 PM #31
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
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.
- 09-26-2010, 08:48 PM #32
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
! 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 (:
- 09-26-2010, 09:33 PM #33
Similar Threads
-
Huge problem. PacMan game.
By Bullfrog in forum New To JavaReplies: 3Last Post: 06-01-2010, 02:39 PM -
Need smart solution on java game lag problem..
By Addez in forum New To JavaReplies: 0Last Post: 11-13-2009, 12:48 PM -
SUDOKU game problem
By bumblyb33 in forum AWT / SwingReplies: 2Last Post: 05-05-2009, 11:18 PM -
Problem with an assignment: Backgammon game
By Poddy in forum New To JavaReplies: 6Last Post: 02-05-2009, 05:32 AM -
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05-05-2008, 04:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks