i have a class enemyboss and sending it to different class called levelObject.

main.java

EnemyBoss enemyObject;
...
//game loop - actionperformed
  levelObject.nextLevel(enemyObject);
  ...

  //paint method 
  if(enemyObject!=null){
    enemyObject.paint(g);
  }

in level.java class i am createding enemyobject class.
level.java

...
enemyObject = new EnemyBoss(50, 20);
...

the problem is nothing prints on screen. its bc enemyObject is null in main method, even though i am createing in level.java. any idea how can i fix this? i want set enemyboss class in main and create in level class.

Recommended Answers

All 2 Replies

either you instantiate it in your main method, or you call the instance from your level class). but why are you passing a reference of which you are sure it'll always be null to a method? that's pointless.

MainClass.java:
EnemyBoss enemy;

Level.java:
EnemyBoss enemy = new EnemyBoss(5, 10);

just because enemy has a value in Level.java, doesn't mean the same goes for the enemy instance in your main class. MainClass.java doesn't even know the enemy instance in Level.java exists.

Hi game06.
Beware incorrect Java terminiology that makes it really hard to understand your questions.

A class is something you define in your source code. The new keyword creates an instance , not a class. Many of your uses of class above should be instance (I think).
You would say "sending" an instance when you pass that instance as a parameter in a method call. If you refer to an instance in another class's code you would call that "refering to" rather than "sending". It's like the difference between push and pull. I don't know which you are really talking about in your post.

Please don't think I'm just being picky here - you will definitely get a better response to your questions when they are easy to understand for people who use standard Java terminology

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.