Results 1 to 7 of 7
Thread: Beginners fault
- 06-23-2011, 06:09 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
Beginners fault
Hello everyone,
My name is Norman I'm from Holland and I study Management Information Technology @ Hogeschool Arnhem Nijmegen. I'll make a good opening post in the right forum. But for now I've a beginners problem with a NullPointerException in a game I must create. I will explain it:
Error:
Here's my ClassJava Code:Start ruimte zoeken en x,y is 10,0 alles: 20,null nullpointerexeption fout java.lang.NullPointerException at Bubblegame.Hangbubble.zoekRuimte(+137) at Bubblegame.Hangbubble.<init>(+78) at Bubblegame.Bubblegame.startGame(+52) at Bubblegame.Bubblegame.menuAction(+61) at phonegame.GameEngine$GameWindow.commandAction(+16) at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282) at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10) at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68) at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)
I found as you can see that my 'mijnspel' has a null value when i run the game. Only I did declared it in my constructor right? I don;t see where this error exists.Java Code:public class Hangbubble extends Bubble{ [LEFT] private int points; public static int kleur; private int x ; private int y ; private Bubblegame mijnspel; public Hangbubble(Bubblegame mijnspel) { this.mijnspel = mijnspel; System.out.println("Klasse hangbubble wordt aangeroepen " + mijnspel); points = 20; x=10; y=0; for (int i = 0; i< 23;i++ ) { Bubble bb = new Bubble(); bb.kleurkiezen(mijnspel); setImage(bb.s); zoekRuimte(); bb.setPosition(x,y); mijnspel.addGameItem(bb); } } public void zoekRuimte() { int n=10; System.out.println("Start ruimte zoeken en x,y is " + x + "," + y ); System.out.println("alles: " + points + "," + mijnspel ); if (mijnspel == null) { System.out.println ("nullpointerexeption fout"); } while (mijnspel.findItemAt(x,y,getFrameWidth(),getFrameHeight()) != null ) { x=x+n; System.out.println("Iets gevonden"); zoekRuimte(); } if (mijnspel.findItemAt(x,y,getFrameWidth(),getFrameHeight()) == null ) //Kijkt of er bubble op de x-as zit { if (mijnspel.findTilesAt (x,y,getFrameWidth(),getFrameHeight()) == 0 ) // Als er geen bubble op locatie ... zit dan wordt daar een bubble geplaatst { System.out.println("Coordinaten om te plaatsen" + getX() + "," + getY() ); x=getX(); y=getY(); } else { x=10; y=y+n; zoekRuimte(); } } }[/LEFT]
Could anyone point me in the right direction, cause I'm trying hours and hours to solve it.
I would be very thankfull!
Norman
- 06-23-2011, 06:24 PM #2
You must give it a non-null value to keep from getting a NullPointerException.Only I did declared it in my constructor right?
Where do you give the variable at line: Bubblegame.Hangbubble.zoekRuimte(+137)
a value?
- 06-23-2011, 06:25 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Yep, most likely (your own program says so) 'mijnspel' equals null. You pass it to your constructor so a null value is passed to it but you didn't show us the code where you are trying to create a new HangBubble object ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-23-2011, 06:45 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
I create a new Hangbubble object in the Bubblegame class using this method
And i don't understand the error code in the console because: Bubblegame.Hangbubble.zoekRuimte(+137)Java Code:public void startGame() { System.out.println ("Startgame wordt aangeroepen" ); setBackgroundImage("/images/background.png",true); //Setbackgroundimage (mogelijkheid met randomselect System.out.println("Achtergrond geplaatst"); //meerdere achtergronden) Klok = new Controller(this); for (int i=0; i <6;i++) {Hangbubble = new Hangbubble(mijnspel);} System.out.println ("Hangbubbles geplaatst");
is at line 137? My whole Hangbubble class only contains 104 lines??? Where does it point to?
- 06-23-2011, 06:54 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-23-2011, 07:23 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
mijnspel references to Bubblegame and I found one error that I adjusted above. In the class bubblegame class I'd reference to bubblegame so that was double. I changed it to 'this' and now it seems to work.Java Code:public void startGame() { System.out.println ("Startgame wordt aangeroepen" ); setBackgroundImage("/images/background.png",true); //Setbackgroundimage (mogelijkheid met randomselect System.out.println("Achtergrond geplaatst"); //meerdere achtergronden) Klok = new Controller(this); for (int i=0; i <6;i++) {Hangbubble = new Hangbubble([COLOR="yellow"]this[/COLOR]);} System.out.println ("Hangbubbles geplaatst");
Yes, it was the latest/current version of my code. Up to the next thing to debug.
But thank you very much all of you that helped! (But i don't think this would be the last question
) we'll see
- 06-23-2011, 07:28 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
SaxUnmarsheller, fault with using a instance... pls help
By naked11 in forum Advanced JavaReplies: 0Last Post: 04-24-2011, 10:25 AM -
Help with fault tolerance for simple program?
By harvest117 in forum New To JavaReplies: 4Last Post: 03-03-2011, 08:59 PM -
[RPC Fault faultString="org.springframework.dao.DataAccessRes ourceFailureException :
By kminev in forum Advanced JavaReplies: 5Last Post: 10-28-2009, 01:59 PM -
segmentation fault error
By Mrs. Deswal in forum NetworkingReplies: 0Last Post: 08-27-2009, 07:44 AM -
'Incompatible operand' fault
By fatnic388 in forum New To JavaReplies: 3Last Post: 12-01-2007, 07:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks