jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Newbie ; NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newbie ; NullPointerException" Watch "Newbie ; NullPointerException" New topic

Newbie ; NullPointerException

Jamie P

Joined: Apr 02, 2001
Posts: 1
Hi there,
I'm a VB programmer trying to learn some java.
So I'm trying to make a game in which one has to solve mazes.
I am coming across a null pointer exception, and I dont know why.
The exception is thrown in the
public void paint(Graphics g) {
event/method dooda.
The code, along with 2 files it uses can be downloaded from here :
Thank you anyone that can solve this.
- jamie
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
A null pointer exception occurs when you try to run a method against an object that has not be instantiated.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Jamie.
And to the rest of you, I'm sorry, this explanation won't make much sense if you haven't looked at Jamie's code.
This is the line that is giving you trouble:

(I had to use "j" as the array's index instead of "i", because this Web site's UBB software interprets "bracket i closing-bracket" as "italicize".)
The problem is that ActiveMazeNumber equals zero here, but I think you want CurrentMazeNumber (which equals one). When I replaced the above line of code with

your game seemed to run fine: at least, it didn't generate any more NullPointerExceptions.
If you need to clarify between CurrentMazeNumber and ActiveMazeNumber, Jamie, I would definitely add comments to distinguish one from the other. And if you only need one defined, I'd get rid of the other one.
Also, I'm not sure about the line in ParseMapData() where you have

right at the beginning of the method? If your intent is to someday have multiple maze definitions, and then build these arrays from your file mazes.maz, Jamie, I would recommend putting this incrementation somewhere at the end of ParseMapData(). In Java, arrays are zero-based, and not filling the "zeroth" element of your MazeData[] array led to some of the confusion here.
And just a little note, as an aside: instead of having to hard-code pi, you can use the constant Math.PI, from the package java.lang.Math. Also, constants are usually defined as "public static final", and their variables are usually capitalized, e.g.,
<PRE>public static final int PLAYER_MAX_SPEED = 15;</PRE>

You can find some similar coding conventions that Sun recommends here.
Hope this helps, Jamie.
[This message has been edited by Art Metzer (edited April 02, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

Jamie P,
I hate to say this but, your name does not comply with the Javaranch naming guidelines which can be found at http://www.javaranch.com/name.jsp
please register again with a valid name.
In particular you need a first and last name both more than 1 character.

Visit my download page
Jamie Plenderleith

Joined: Apr 03, 2001
Posts: 2
Hows the username now
Anyway, I implimented all/some of your suggestions and have been working on it myself too.
Just wondering now, why is the new code not catching the keyboard events ?
[This message has been edited by Jamie Plenderleith (edited April 03, 2001).]
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Jamie.
I think the loop in the original updateGame() method was throwing off the processing of events.
Try this code:

The main things I did were remove the loop in updateGame(), and instead call updateGame() whenever a key event is handled. I also removed your program's flicker.
I got rid of TextMan (Java has methods to handle String parsing), but I did refer to a new class I based on TextMan called StringToInts, that takes a comma-delimitered String of integers and converts it to an array of ints:

I'm glad you're venturing from the realm of VB to the realm of Java, Jamie. I would urge you to investigate and use the Java API: it'll get you up to speed faster. As you become more familiar with Java, you may want to revisit this maze program and analyze how you might make it object-oriented.
Fun game, that little pointer is hard to control!
Jamie Plenderleith

Joined: Apr 03, 2001
Posts: 2
Hey thanks for the code.
Its very good.
I have implimented (well, copy+pasted ) the code, and its working great. I have the triangle's speed set to 0 after each update so it makes it a lot easier to control.
One thing though, is there no way of getting the origninal RunGameLoop() to function ? In VB you'd just stick in the DoEvents keyword which would then pass control to other events/threads/programs for a short period of time.
I was taking a look at processKeyEvents() and things like that. Would those not be what I'm after ?
Anyway, thanks again for the excellent code.
Have you checked out Aspose?
subject: Newbie ; NullPointerException
It's not a secret anymore!