• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

I'm fazed by a nullpointererror I can't get rid of

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's really annoying because it seems logical enough to me and I'm sure it's something really simple. I hope someone can help me. It's a bat and ball game to which I'm adding a computer opponent. The problem occurs in the Ball code:



and throws the exception because it doesn't like aii.b. But it has no problem in the next if statement:
if ((int)(x)>=490 && y>james.z-6 && y<james.z+35+6)

which is exactly the same logic.


Here's the main programme


And here's the Ball code...



Here's the Player code...



And finally here's the AI code...

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in batball3's init()

ball=new Ball(200,250,-5,-5,bat,hitwall,score,missit,ai)
ai=new Ai(ball);

ai is null - used prior to it's creation

if you change the lines around, ball will be null - same reason
 
Martin Platt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael, this makes sense. I see what you mean that, either way, you get the error. But I need ai to know about ball, and ball to know about ai. Is there an easy way to send this information after the object is created?
[ December 28, 2005: Message edited by: Martin Platt ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could try this (need to modify ball's constructor)

//ball=new Ball(200,250,-5,-5,bat,hitwall,score,missit,ai);//remove ai
ball=new Ball(200,250,-5,-5,bat,hitwall,score,missit);
ai=new Ai(ball);
ball.aii = ai;
 
Martin Platt
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much! It works.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic