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: Jva Game error: Exection in thread "main" java.lang.nullpointerexception

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Jva Game error: Exection in thread "main" java.lang.nullpointerexception

    So basically i' m following a youtube series on making a 2d game and i 've placed some code in but i get this annoying error, HELP ME PLEASE:

    package com.weebly.alfiemartin.Game;

    import java.awt.*;
    import javax.swing.*;

    public class gMain extends Canvas implements Runnable{

    private static final long serialVersionUID = 1L;
    public static int width = 300;
    public static int height = width / 16 * 9;
    public static int scale = 3;

    private Thread gameThread;
    private boolean running = false;
    private JFrame window = new JFrame();

    public gMain(){
    Dimension screenSize = new Dimension(width*scale, height * scale);
    setPreferredSize(screenSize);

    window = new JFrame();

    }

    public synchronized void start(){
    running = true;
    gameThread = new Thread(this, "Display");
    gameThread.start();
    }
    public synchronized void stopThread(){
    running = false;
    try{
    gameThread.join();
    }catch(InterruptedException e){
    e.printStackTrace();
    }
    }
    public void run(){
    while(running){
    System.out.println("Hello My Name Is Alfie");
    }

    }

    public static void main(String args[]){
    gMain gMain = new gMain();
    gMain.window.setResizable(false);
    gMain.window.setTitle("First Game");
    gMain.window.add(gMain);
    gMain.window.pack();
    gMain.window.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE);
    gMain.window.setLocation(null);
    gMain.window.setVisible(true);

    gMain.start();


    }

    }




    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Component.setLocation(Unknown Source)
    at java.awt.Window.setLocation(Unknown Source)
    at com.weebly.alfiemartin.Game.gMain.main(gMain.java: 52)




    Any help is appreciated, thank you!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Jva Game error: Exection in thread "main" java.lang.nullpointerexception

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Component.setLocation(Unknown Source)
    at java.awt.Window.setLocation(Unknown Source)
    at com.weebly.alfiemartin.Game.gMain.main(gMain.java: 52)
    There is a variable or value with a null value on line 52. Look at line 52 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 52 and print out the values of all the variables on that line.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jva Game error: Exection in thread "main" java.lang.nullpointerexception

    on line 52 the code sets the location of the jframe window to null aka centre-screen so i changed it to (0,0) thankyou now its working,THanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Jva Game error: Exection in thread "main" java.lang.nullpointerexception

    There is a method that will center a JFrame when given a null arg. Check the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Jva Game error: Exection in thread "main" java.lang.nullpointerexception

    Quote Originally Posted by Amartin4200 View Post
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Component.setLocation(Unknown Source)
    at java.awt.Window.setLocation(Unknown Source)
    at com.weebly.alfiemartin.Game.gMain.main(gMain.java: 52)
    yes you have made the mistake on this line
    gMain.window.setLocation(null);

    your code set the null location. So you have got the null pointer exception

    you set any values in location like this
    gMain.window.setLocation(300, 200);

    And you refer this link: JFrame: setLocation(int x, int y) : JFramejavax.swingJava by API
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM