• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Game of Life Help  RSS feed

 
Kevin Stock
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!