![]() |
|
[Java] Cant put object into hashmap connected to other object - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Cant put object into hashmap connected to other object (/showthread.php?tid=5927877) |
Cant put object into hashmap connected to other object - deadsh0t - Jan 9, 2019 So im making this consolebased game for my software development class as my assessment But i cant understand this bit or why it is wrong to do it like this. So basicly i have 10 rooms so a room has the following things: -A roomnumber -A hashmap that contains a Room as value and a String as key -An arraylist for items (Item has its own class ofcourse) Now the problem, A room can be exited from 4 direction North,East,South,West so thats the String/key in the hashmap It can also have 2 exits or 1 or 3. So in that hashmap i save the direction as key and as value another room object so i can keep track wich exit goes to wich room. so i have a method for adding exits. that looks like this: Code public class Room {And than in the game class i create 10 rooms and use call the addexits method to add exits that looks like this: Code Rooms = new ArrayList<Room>();However as you can see with room1 i try to add 2 exits 1 in the west to room4 and 1 in the east to room3 however when i run this it shows this in the hashmap: West -> null East -> null But when i add 2 exits both with object room1 it puts it like it should be like this: West -> room1 East -> room1 So i dont really get what im doing wrong each room has its own hashmap on creation and the rooms are already created so that shouldnt be an issue. RE: Cant put object into hashmap connected to other object - GenericSkid - Jan 10, 2019 Your rooms are null when you add them... initialize all the rooms first THEN add them as exits.
PHP Code RE: Cant put object into hashmap connected to other object - deadsh0t - Jan 10, 2019 (Jan 10, 2019 03:37 AM)GenericSkid Wrote: Your rooms are null when you add them... initialize all the rooms first THEN add them as exits. Yeah i figured it out i felt so dumb haha Yeah the camalCaseStyle is something that is a must in my assingment i really need to stick to it,but sometimes i forget to use it. Thanks for the tip on the map and making it final :) |