Java assignment problems.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
For some reason my program isn't assigning the NESW variables properly to the objects in the graph, it just prints out null everytime I try to find out what it's North value is.

Code:
import java.io.*;

public class Main {

	Location Albus;
	Location C;
	Location B;
	Location Frank;
	Location George;
	Location D;
	Location Library;
	Location Reading;
	Location Grizedor;
	Location Furness;
	Location Current;
	
	String inventory;
		
	BufferedReader input;
	
	public Main() {
		Albus = new Location("Albus Square", "Flowers, Traffic Cone", Frank, null, Furness, Library, null, null, null);
		C = new Location("C Floor Infolab", null, null, null, null, null, D, B, null);
		B = new Location("B Floor Infolab", null, Grizedor, null, null, null, C, null, null);
		Frank = new Location("Franklandclaw Lecture Theatre", "Coursework", null, null, Albus, null, null, null, null);
		George = new Location("George Fox", "Pen", Furness, null, Grizedor, null, null, null, null);
		D = new Location("D Floor Infolab", null, null, null, null, null, null, C, null);
		Library = new Location("Library of Enchanted Books", null, null, Albus, null, null, Reading, null, null);
		Reading = new Location("Reading Room", "Book, Magic Ink", null, null, null, null, null, Library, null);
		Grizedor = new Location("Grizedor College", "Empty Beer Glasses", George, null, B, null, null, null, null);
		Furness = new Location("Furnesspuff College", "Half Eaten Kebab", Albus, null, George, null, null, null, null);
		Current = Albus;
		
		input = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println(Albus.North);
		
		displayLocation();
		readCommand();
	}
	
	public void readCommand() {	
		String data = null;
		
		try {
			data = input.readLine();
		} catch (IOException io4) {
			System.out.println("Input Error!");
		}	
		
		if (data.contains("GO")) {
			go(data);
		} else if (data.contains("TAKE")) {
			take("");
		} else if (data.contains("DROP")) {
			drop("");
		} else if (data.contains("GIVE")) {
			givechar("", "");
		} else if (data.contains("USE")) {
			use("", "");
		} else if (data.contains("INV")) {
			inv();
		} else {
			System.out.println("Bad Command");
		}
	}
	
	public void go(String direction) {
		try {
			direction = direction.substring(3, 7);
		} catch (Exception e) {
			System.out.println("Bad direction!");
			System.exit(1);
		}
		
		if (direction.equals("Nort")) {
			if (Current.North != null) {
			Current = Current.North;
			displayLocation();
			} else {
				System.out.println("There's nothing there!");
			}
		} else if (direction.equals("Sout")) {
			if (Current.South != null) {
			Current = Current.South;
			displayLocation();
			} else {
				System.out.println("There's nothing there!");
			}
		} else if (direction.equals("East")) {
			if (Current.East != null) {
			Current = Current.East;
			displayLocation();
			} else {
				System.out.println("There's nothing there!");
			}
		} else if (direction.equals("West")) {
			if (Current.West != null) {
			Current = Current.West;
			displayLocation();
			} else {
				System.out.println("There's nothing there!");
			}
		} else {
			System.out.println("Bad direction!");
		}
	}
	
	public void displayLocation() {
		System.out.println("Welcome to BogWalks School of Computer Science and Programming. \n");
		System.out.println("You are in: " + Current.name + "\n" + "Items: " + Current.items);
	}	
	
	public void take(String item) {
	}
	
	public void drop(String item) {
	}
	
	public void use(String item1, String item2) {
	}
	
	public void inv() {
	}
	
	public static void main(String[] args) {
		Main main = new Main();
	}
}

Code:
public class Location {
	public String name;
	public String items;
	public Location North;
	public Location East;
	public Location South;
	public Location West;
	public Location Up;
	public Location Down;
	
	public Location(String name, String items, Location North, Location East, Location South, Location West, Location Up, Location Down, Character character) {
		this.name = name;
		this.items = items;
		this.North = North;
		this.East = East;
		this.South = South;
		this.West = West;
		this.Up = Up;
		this.Down = Down;
	}	
}
 
Last edited:
At the time of creation of the Location 'Albus', everything apart from the first two parameters are equal to null. The problem lies with the Java pass by value parameter system, you need to rethink your design.

Don't assign the location in the constructor, add a method to do it called add locations or something. You can then call this method after all of the locations have been created.

For example:
Code:
Location L1 = new Location("Name", "Items")
// ...
// Other Location declarations omitted
// ...
L1.addDirections(<North>, <East>, <South>, <West>);

The stuff in <..> are placeholders for the location objects.
 
Last edited:
Back
Top Bottom