Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Having trouble with code for my game

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with code for my game

    I continue to get this error:
    Exception in thread "main" java.lang.NullPointerException
    at Main.GamePanel.<init>(GamePanel.java:106)
    at Main.Game.main(Game.java:10)

    This is the code:
    Game Panel
    <code>package Main;

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;

    import javax.swing.JPanel;

    import GameState.GameStateManager;


    public class GamePanel extends JPanel implements Runnable, KeyListener{

    //dimensions
    public static final int WIDTH = 320;
    public static final int HEIGHT = 240;
    public static final int SCALE = 2;

    //game thread
    private Thread thread;
    private boolean running;
    private int FPS = 60;
    private long targetTime = 1000 / FPS;

    //image
    private BufferedImage image;
    private Graphics2D g;

    //game state manager
    private GameStateManager gsm;

    public GamePanel() {
    super();
    setPreferredSize (new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setFocusable(true);
    requestFocus();
    }

    public void addNotify() {
    super.addNotify();
    if(thread == null) {
    thread = new Thread(this);
    addKeyListener(this);
    thread.start();
    }
    }

    private void init() {

    image = new BufferedImage(
    WIDTH, HEIGHT,
    BufferedImage.TYPE_INT_RGB
    );

    g = (Graphics2D) image.getGraphics();
    running = true;

    gsm = new GameStateManager();

    }


    public void run() {

    init();

    long start;
    long elapsed;
    long wait;

    //game loop
    while(running) {

    start = System.nanoTime();

    update();
    draw();
    drawToScreen();

    elapsed = System.nanoTime() - start;

    wait = targetTime - elapsed / 1000000;


    try {
    Thread.sleep(wait);
    }
    catch(Exception e) {
    e.printStackTrace();
    }

    }


    }

    private void update() {
    gsm.update();
    }
    private void draw() {
    gsm.draw();
    }

    private void drawToScreen() {} {
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    }


    public void keyTyped(KeyEvent key) {}
    public void keyPressed(KeyEvent key) {
    gsm.keyPressed(key.getKeyCode());
    }
    public void keyReleased(KeyEvent key) {
    gsm.keyReleased(key.getKeyCode());
    }


    }
    </code>
    Game
    <code>package Main;

    import javax.swing.JFrame;

    public class Game {

    public static void main (String[] args) {

    JFrame window = new JFrame("Game");
    window.setContentPane(new GamePanel());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setResizable(false);
    window.pack();
    window.setVisible(true);



    }
    }
    </code>
    any help will be greatly appreciated


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Having trouble with code for my game

    Please use code tags when posting code. Use [ ] not < >

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    That line should of thrown an error.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with code for my game

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags which are explained near the top of the above link.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with code for my game

    Quote Originally Posted by Ada Lovelace View Post
    Please use code tags when posting code. Use [ ] not < >



    That line should of thrown an error.

    Wishes Ada xx
    I continue to get this error:
    Exception in thread "main" java.lang.NullPointerException
    at Main.GamePanel.<init>(GamePanel.java:106)
    at Main.Game.main(Game.java:10)

    This is the code:
    Game Panel
    package Main;
     
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.event.*;
     
    import javax.swing.JPanel;
     
    import GameState.GameStateManager;
     
     
    public class GamePanel extends JPanel implements Runnable, KeyListener{
     
    //dimensions
    public static final int WIDTH = 320;
    public static final int	HEIGHT = 240;
    public static final int SCALE = 2;
     
    //game thread
    private Thread thread;
    private boolean running;
    private int FPS = 60;
    private long targetTime = 1000 / FPS;
     
    //image
    private BufferedImage image;
    private Graphics2D g;
     
    //game state manager
    private GameStateManager gsm;
     
    public GamePanel() {
    super();
    setPreferredSize (new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setFocusable(true);
    requestFocus();
    }
     
    public void addNotify() {
    super.addNotify();
    if(thread == null) {
    thread = new Thread(this);
    addKeyListener(this);
    thread.start();
    }
    }
     
    private void init() {
     
    image = new BufferedImage(
    WIDTH, HEIGHT,
    BufferedImage.TYPE_INT_RGB
    );	
     
    g = (Graphics2D) image.getGraphics();
    running = true;
     
    gsm = new GameStateManager();
     
    }
     
     
    public void run() {
     
    init();
     
    long start;
    long elapsed;
    long wait;
     
    //game loop
    while(running) {
     
    start = System.nanoTime();
     
    update();
    draw();
    drawToScreen();
     
    elapsed = System.nanoTime() - start;
     
    wait = targetTime - elapsed / 1000000;
     
     
    try {
    Thread.sleep(wait);
    }
    catch(Exception e) {
    e.printStackTrace();
    }
     
    }
     
     
    }
     
    private void update() {
    gsm.update();
    }
    private void draw() {
    gsm.draw();
    }
     
    private void drawToScreen() {} {
    Graphics g2 = getGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    }
     
     
    public void keyTyped(KeyEvent key) {}
    public void keyPressed(KeyEvent key) {
    gsm.keyPressed(key.getKeyCode());
    }
    public void keyReleased(KeyEvent key) {
    gsm.keyReleased(key.getKeyCode());
    }
     
     
    }
    Game
    package Main;
     
    import javax.swing.JFrame;
     
    public class Game {
     
    public static void main (String[] args) {
     
    JFrame window = new JFrame("Game");
    window.setContentPane(new GamePanel());
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    window.pack();
    window.setVisible(true);
     
     
     
    }
    }
    Fixed that CLO SE problem but it still outputs the same error

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Having trouble with code for my game

    Good job on the code tags, now please post properly indented code.

    Your while(true)/Thread.sleep() game loop is not appopriate for a Swing application. You can find several good articles on why this is not a good idea and other articles that walk through the development of a good/better/best game loop with clear justification for each improvement.

    Swing is not thread safe, and a Swing app should be run on the Event Dispatch Thread or "EDT". You can find out more about these topics by searching the 'net.

    Your error occurs because there is a null object at line 106 of GamePanel. Look at line 106, identify all of the objects in that line, and using print statements or intuition, figure out which of the objects is null. Then work backwards to determine why that object is null and fix it.

Similar Threads

  1. having trouble with my code
    By JavaNewbie12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2014, 02:34 AM
  2. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Having Trouble Modify the code!!
    By jehov86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 09:42 AM
  5. Having trouble ending a game
    By wakaxwaka in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2012, 06:33 PM