• 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

Newbie ; NullPointerException

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
http://209.68.222.201/game.zip
Thank you anyone that can solve this.
- jamie
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A null pointer exception occurs when you try to run a method against an object that has not be instantiated.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Jamie:
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.
Art
[This message has been edited by Art Metzer (edited April 02, 2001).]
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ? http://209.68.222.201/game.zip
Thanks,
jamie.
[This message has been edited by Jamie Plenderleith (edited April 03, 2001).]
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Art
 
Jamie Plenderleith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Regards,
Jamie.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic