Dear Daniwebbers,

I have a problem regarding the calling of methods on a protected parent object from within a child object, the protected parent object being a child object of the same class.

I have a class HostileArea, wich has a "protected DungeonRoom[][] dungeon", DungeonRoom being a child of HostileArea.
From within one such DungeonRoom object in the dungeon variable, I want to get a function foo on another DungeonRoom in the dungeon variable. Only problem being, it won't let me.
The IDE signals no problems, it's only with trying to access a parent object from within a same object that gives problems. It doesn't even matter if dungeon is an array, or just a single object.

During debugging the program runs without doing anything when trying to access, at least so far I can see.

I honestly have no idea why this is happening, perhaps one of you friendly folk can help me out. :)


Recommended Answers

All 5 Replies

so, if I understand this correctly, you have something like

HostileArea > containing a row of DungeonRoom objects
DungeonRoom extends HostileArea

well ... the first element can't make the second element of your row perform an action, you'll need the actual second element to do that.

what do you actually mean? can you submit your code here, because I hardly understand what you've done, and what I (think to) understand, seems like a very weird and bad structure to me.

if you have a two-dimensional array of DungeonRoom objects in HostileArea, and DungeonRoom extends HostileArea => for each instance of DungeonRoom, you're creating a two dimensional array, and I assume that's not what you're looking to do

I don't understand your last sentence. Why does it make an array for every DungeonRoom ?

public class HostileArea extends Location {
	private String naam,filename,image,dungeonMap;
	File story;
	protected DungeonRoom[][] dungeon;
	private int ID;
	private int[] startPos;
public HostileArea(Integer id,Integer x,Integer y,String name,String filename,String image,String dungeonMap){
		ID = id.intValue();
		positie[0] = x;
		positie[1] = y;
		naam = name;
		this.image = image;
		this.filename = filename;
		story = new File(this.filename);
		this.dungeonMap = Global.rwtext.getContents(new File("Data/DungeonMaps/" + dungeonMap));//
public void buildMap(){
		//get # rows and columns
		String[] lines = dungeonMap.split(System.getProperty("line.separator"));
		int numLines = lines.length;
		int width = 0;
		for(String str: lines){
			if(str.length() > width) width = str.length();
		dungeon = new DungeonRoom[width][numLines];
                // here each of them gets initialized according to their type, be it forest, dungeon or whatever
public class DungeonRoom extends HostileArea{
	String type = null;//forest, dungeon, path
	DungeonEvent event = null;
	Enemy mob;
	int[] position;
	public DungeonRoom(){
	public DungeonRoom(String type,Enemy mob, DungeonEvent event,int[] position){
		this.type = type;
		this.mob = mob;
		this.event = event;
		this.position = position;
	public void enter(String origin){
		if(mob != null){
			//mob present, combat
		if(event != null){
			//play dungeon event
			//just road
			// 1) check for crossroad
			for(int j=-1;j<2;j++){
				for(int k=-1;k<2;k++){
					if(dungeon[position[0] + j][position[1] + k].getType().equalsIgnoreCase("path") && k!=0 && j!=0){
						RPGMain.printText(true,"Go to the " + Town.getDirection(j, k) + ".");
			// 2) check to see enemies ahead (difference day/night)
				// can only see ahead during the day
				//origin is THE DIRECTION YOU CAME FROM, ie coming from the east -> you went to the west
				for(int j=-1;j<2;j++){
					System.out.println("Testing see enemies");
							RPGMain.printText(true, "To the " + Town.getDirection(j, 1) + " you see an " + dungeon[position[0]][position[1]+1].getMob().getNaam()+ ".");
							RPGMain.printText(true, "To the " + Town.getDirection(j, -1) + " you see an " + dungeon[position[0]][position[1]-1].getMob().getNaam()+ ".");
							RPGMain.printText(true, "To the " + Town.getDirection(-1, j) + " you see an " + dungeon[position[0]-1][position[1]].getMob().getNaam()+ ".");
							RPGMain.printText(true, "To the " + Town.getDirection(1, j) + " you see an " + dungeon[position[0]+1][position[1]].getMob().getNaam()+ ".");
//and some getters etc

The problem lies whenever I try to access dungeon in the DungeonRoom object the player is in.
The DungeonMap is made through a .txt file looking something like this:


with f being forest, s being starter point, p being path, and numbers the IDs of the enemies standing there

The problem lies whenever I try to access dungeon in the DungeonRoom object the player is in

What does that code look like?

Is it possible you are accessing the dungeon array from your current DungeonRoom frather than that in the "parent" HostileArea? That would explain lack of error messages but no output.
If so you need DungeonRoom to have an instance variable for the HostileArea it's part of, and you can pass that in to the constructor for DungeonRoom when you create them following line 28.

It's perfectlt legal, but quite confusing having HostileArea as the superclass of DungeonRoom, but also a instance of HostileArea containing instances of DungeonRoom. Does DungeonRoom really have to extend HostileArea?

What does that code look like?

For example in my DungeonRoom enter method, at line 30, when i try to access the surrounding DungeonRoom objects in my 2D array dungeon.

And I think I finally got what the second poster was saying, with JamesCherrill triggering it by asking if it was the parent dungeon I was adressing. By making DungeonRoom a child of HostileArea, the child also gets the variable dungeon. D'oh, what a silly mistake..
It was so obvious I constantly kept looking over it.

And the main reason for making DungeonRoom a child was easy access to the parent dungeon.. :P Must've been some godforsaken hour when I decided to make it a child.

Thanks to you both, even long after our beginner phases we sometimes still make beginner mistakes but just look over it because we're looking for a more complex problem. Sigh

Much regards,

And the main reason for making DungeonRoom a child was easy access to the parent dungeon.. :P Must've been some godforsaken hour when I decided to make it a child.

Excellent! ps where you need to access vars from a containing Object like that you can make the contained class an inner class of the containing class so it has access to the variables of the instance that contains it.

commented: You answered my question before I even got to ask it, great! +2
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.