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 7 of 7

Thread: Game of Life GUI Error

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Game of Life GUI Error

    SOLVED.

    I'm attempting to build a GUI for a console-based Game of Life game I've already developed. I went about it by building the very basic GUI (a grid with red squares that are either on or off with JButtons to control), then integrating a Game of Life concept into it (where I render the original grid with a random feature, then analyse it and create a new tempgrid with the new data, clear the original and interchange them, then clear the tempgrid).

    The GUI runs, but when starting the game via the JButton the following error occurs:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ClickGame.prepareNextGeneration(ClickGame.java:21)
    	at StartListener.actionPerformed(StartListener.java:27)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    I've checked and re-checked my code, and I genuinely have no idea on where to start fixing this error, the text version runs perfectly but this refuses to work even though I use the exact same concepts. If you require any more of the classes involved, please ask as I'll be watching this thread all day and can reply instantly.

    Thank you in advance.
    Last edited by Lavace; January 3rd, 2011 at 09:16 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Game of Life GUI Error

    Here is the pertinent line of the stack trace: at ClickGame.prepareNextGeneration(ClickGame.java:21)

    That's saying your NPE is at line 21 of ClickGame in the preprateNextGeneration() method. Which line is that? Put in some print statements before that line (or use a debugger) to figure out exactly what's going wrong.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game of Life GUI Error

    Quote Originally Posted by KevinWorkman View Post
    Here is the pertinent line of the stack trace: at ClickGame.prepareNextGeneration(ClickGame.java:21)

    That's saying your NPE is at line 21 of ClickGame in the preprateNextGeneration() method. Which line is that? Put in some print statements before that line (or use a debugger) to figure out exactly what's going wrong.
    Hi Kevin,

    I inserted print messages for the StartListener actionPerformed method, inside prepareNextGeneration and inside the loops.

    I find that it stops executing at the stated point:
    ' for (int x = 0; x < c.getX(); x++){ '

    Could it be posisbly due to being unable to retreive the value of c.getX();?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Game of Life GUI Error

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Game of Life GUI Error

    Quote Originally Posted by Lavace View Post
    Hi Kevin,

    I inserted print messages for the StartListener actionPerformed method, inside prepareNextGeneration and inside the loops.

    I find that it stops executing at the stated point:
    ' for (int x = 0; x < c.getX(); x++){ '

    Could it be posisbly due to being unable to retreive the value of c.getX();?
    You set the value of c in the constructor. But when do you call that constructor? You use that variable in a static method, which means that you aren't guaranteed to have ever set it. Make sure you understand the difference between static and non-static before you use that keyword.

    That's the gist of your problem. I'm not sure where else you've crossposted the question and what help you've already received, so I'm going to leave it at that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game of Life GUI Error

    Quote Originally Posted by KevinWorkman View Post
    You set the value of c in the constructor. But when do you call that constructor? You use that variable in a static method, which means that you aren't guaranteed to have ever set it. Make sure you understand the difference between static and non-static before you use that keyword.

    That's the gist of your problem. I'm not sure where else you've crossposted the question and what help you've already received, so I'm going to leave it at that.
    Hi Kevin,
    I done some research on your suggestion and realised that I had declared the preperation method static (because I was re-using this code from my console version).

    As a consequence, I had to rename all the methods to static in ClickData, change the instance variables to static and as I've just learnt, change the way the methods are called.

    But I do not understand why if I remove the static references, the code still points to the same error (however, only to the prepareNextGeneration() method and no references to where).

    Thank you ever so much for your help, I've been stuck on this for days.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Game of Life GUI Error

    I hesitate to spend too much time on this post since you crossposted to at least two other sites, so I'll just reiterate: make sure you understand the difference between static and non-static.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. error with a java game..(beginner)
    By Skat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2010, 02:04 PM
  2. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM
  3. help help, ... :) "game of life"
    By sanfor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2009, 12:40 PM
  4. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM