Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help using extends  (Read 1059 times)
0 Members and 1 Guest are viewing this topic.
Offline syrinth

Senior Newbie





« Posted 2011-08-26 02:58:15 »

Hello, I'm hoping I can get some help with this...

I have several different objects who all share the same code. I am originally coding each object separately but find the fact that I am writing out the exact same methods in each object to be frustrating. I tried compiling all the methods in a superclass Actor which the other classes extends but I am getting a null pointer exception error when I attempt to access a field of the subclass.

Here is the subclass code Fighter which extends Actor. Actor contains all the getters and setters. The default constructor for the Actor method does nothing currently. Can you tell me what I'm doing wrong?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
public class Fighter extends Actor{
   private Node node, target;
   private int x, y, width, height;
   private Image image;
   private String src;
   private double speed;
   private ArrayList<Node> path;
   private ArrayList<Node> open;
   private ArrayList<Node> closed;

   public Fighter(Node node){
      System.out.println("MAR");
      src = "/resources/fighter.gif";
      path = new ArrayList<Node>();
      open = new ArrayList<Node>();
      closed = new ArrayList<Node>();
      this.node = node;
      this.node.setActor(this);
      target = null;
      speed = 2;
      x = node.getCentreX();
      y = node.getCentreY();
     
      ImageIcon ii = new ImageIcon(this.getClass().getResource(src));
      image = ii.getImage();
     
      width = image.getWidth(null);
      height = image.getHeight(null);
   }
}

public class Actor{
   private Node node, target;
   private int x, y, width, height;
   private Image image;
   private String src;
   private double speed;
   private ArrayList<Node> path;
   private ArrayList<Node> open;
   private ArrayList<Node> closed;

   public Actor(){
   }

        ... insert methods here ...
}
Offline gmanKent

Innocent Bystander





« Reply #1 - Posted 2011-08-26 03:31:27 »

You are using variables out of scope. The private variables that you declare in the Actor object are only available within the class, not to any object that inherits the Actor class.

When you declare the same variables in the Fighter object, they are different variables even though they are named the same. It then gets a null pointer because the variables in the Actor class are never set.

Just change the variables in the Actor class to be public and remove the variables from the Fighter class as follows:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public class Fighter extends Actor{
 
    public Fighter(Node node){
        System.out.println("MAR");
        src = "/resources/fighter.gif";
        path = new ArrayList<Node>();
        open = new ArrayList<Node>();
        closed = new ArrayList<Node>();
        this.node = node;
        this.node.setActor(this);
        target = null;
        speed = 2;
        x = node.getCentreX();
        y = node.getCentreY();
         
        ImageIcon ii = new ImageIcon(this.getClass().getResource(src));
        image = ii.getImage();
         
        width = image.getWidth(null);
        height = image.getHeight(null);
    }
}
 
public class Actor{
    public Node node, target;
    public int x, y, width, height;
    public Image image;
    public String src;
    public double speed;
    public ArrayList<Node> path;
    public ArrayList<Node> open;
    public ArrayList<Node> closed;
 
    public Actor(){
    }
 
        ... insert methods here ...
}
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-08-26 06:59:26 »

Just change the variables in the Actor class to be public...
Better yet: protected Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #3 - Posted 2011-08-26 08:17:21 »

2 few thing :
- You can load image via ImageIO

1  
2  
3  
4  
5  
6  
7  
8  
  try
  {
     image = ImageIO.read(this.getClass().getResource(src));
  }
  catch(Exception ex) // Uggly use the right exception
 {
     // Image not loaded
 }


- You reload the image everytime you create a fighter. You can use "static" member to have a member commun to all objects of a class. With this code, you load the image only one time :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public class Fighter extends Actor{
    private static final String SRC = "/resources/fighter.gif";
    private static Image fighterImage = null;

    public Fighter(Node node){
        System.out.println("MAR");
        path = new ArrayList<Node>();
        open = new ArrayList<Node>();
        closed = new ArrayList<Node>();
        this.node = node;
        this.node.setActor(this);
        target = null;
        speed = 2;
        x = node.getCentreX();
        y = node.getCentreY();
         
        if(fighterImage == null)
       {
          try
          {
             fighterImage = ImageIO.read(this.getClass().getResource(SRC));
          }
          catch(Exception ex) // Uggly use the right exception
         {
             // Image not loaded
         }
       }

        image = fighterImage;
         
        width = image.getWidth(null);
        height = image.getHeight(null);
    }
}
 
public class Actor{
    public Node node, target;
    public int x, y, width, height;
    public Image image;
    public String src;
    public double speed;
    public ArrayList<Node> path;
    public ArrayList<Node> open;
    public ArrayList<Node> closed;
 
    public Actor(){
    }
 
        ... insert methods here ...
}
Offline Cero
« Reply #4 - Posted 2011-08-26 12:36:49 »


package protected (no modifier) is underrated =D

Offline syrinth

Senior Newbie





« Reply #5 - Posted 2011-08-26 15:51:26 »

I am so absolutely appalled with myself.

Completely and totally disgusted.

Thank you so much!
Offline Scarzzurs
« Reply #6 - Posted 2011-08-26 15:57:20 »

package protected (no modifier) is underrated =D

But... it looks weird...

<.<

>.>

Also I find it hard to justify package protected...
When is it actually good code to use it? (Stop me if I'm going off road here)

- Scarzzurs

My games and Projects:
BlastingPixels.com,
Old website
Offline Z-Man
« Reply #7 - Posted 2011-08-26 20:46:57 »

I don't know whether or not it's good code, but say you have a package that holds all the Entitys for your game. Maybe you want the Entitys to be able to access methods and fields that the rest of the world shouldn't be able to access. Then it's useful.

EDIT: I worded that a little weird, allow me to clarify. "Maybe you want the Entities to access methods and variables of other Entities that the rest of the world shouldn't have access to."
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-08-27 09:19:56 »

Yeah, a method/variable that you only want another class in the same package to access.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (43 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!