• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Game of Life Help

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

First off, quick introduction:

I'm a high school student entering my senior year, in which I'll be taking computer science AP. Regularly most people take intro to cs, but I'll be skipping that class. As a result I have to learn the basics of java on my own which is proving difficult. I'm using the book "Big Java" which is helpful, but can't cover everything.

So, as part of my learning I'm attempting to write a game of life program but am running into many difficulties with getting the gui to work right. The program complies fine, but upon running it says all sorts of java.lang.NullPointerException at ....

If somebody could help me get this program working I'd be very thankful. If any more info is needed just ask, or I can email anyone who would be willing to look over the source code.

Thanks,
Kevin
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kevin

Welcome to JavaRanch!

Well, those NullPointerException stack traces contain a lot of useful information. There's a list of "stack frames", basically the methods that were running when the error occurred. The method in which the actual error happened is listed first; on the next line is the function that called that one, and so on. Normally you'll also get line numbers. So when you get a NullPointerException stack trace, the first thing to do is look in the named source file at the given line; you'll invariably find a "." (dot) operator on that line. The NullPointerException is telling you that the thing on the left-hand-side of that dot is null -- i.e., no value has been assigned to it. Knowing this, it's often simple to find the place in your code where a value ought to have been assigned, and figure out what went wrong.

You can certainly post code excepts fo advice here if you get stuck. Good luck to you!
 
Kevin Stock
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I found the problem. If I'm correct, a new 2d arraywill be filled with null, not zero. (btw, it's declaired final at this point so that compiler doesn't complain about the inner class mouse listener accessing it, is this also a problem?) If this is the case then I assume the problem is that it can't work with null properly, because the problem start when it trys to first draw the field, which it does by looking at the array.

Thanks for the help,
Kevin
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Starr:
If I'm correct, a new 2d array

final int[][] lifeWorld = new int[WORLD_ROWS][WORLD_COLUMNS];

will be filled with null, not zero.


You are not correct. That is an int array and int is a primitive type. Primitives cannot be null, only object references can.

Try it:


I suggest that you follow Ernest's advice and use the stack trace to find the offending line.
[ July 19, 2004: Message edited by: Darin Niard ]
 
Kevin Stock
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I know for sure now that the problem is somewhere within how I'm using swing. For now I plan on just making it work with simple text output, and once I know the whole deal is working I'll add graphics.

Thanks,
kevin
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Starr:
For now I plan on just making it work with simple text output, and once I know the whole deal is working I'll add graphics.



Learning quick you are, young padawan
 
reply
    Bookmark Topic Watch Topic
  • New Topic