Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Tolls

Thread: Vectors scare me.

  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Vectors scare me.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class init {
    	
    	//Initialize variables
    	private static int choice;
    	private static String name;
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter your name: ");
    		name = sc.nextLine();
    		System.out.println("Character created. \"" + name + "\"");
    		room town = new room("Busy Town", "Everyone is preparing for a holiday, a quite peculiar one. Strange, glowing objects are being carried along by workers.");
    		room magicCircle = new room("Glowing Circle", "A strange circle with foreign symbols is glowing beneath your feet.");
    		room mayorsOffice = new room("Mayors Office", "The office has thick walls, threatening to block out all sound. The mayor sits at his desk.");
    		town.addExit(new exit(exit.NORTH, magicCircle));
    	}
    }
    Java Code:
    import java.util.Vector;
    
    public class room {
    	
    	private Vector vecExit;
    	private String name;
    	private String desc;
    
    	public room(String name, String desc){
    		this.name = name;
    		this.desc = desc;
    	}
    	
    	public String getName(){
    		return name;
    	}
    	
    	public String getDesc(){
    		return desc;
    	}
    	
    	public void addExit(exit exit){
    		vecExit.addElement(exit);
    	}
    	
    	public void removeExit(exit exit){
    		if(!(vecExit.isEmpty())){
    			vecExit.removeElement(exit);
    		}
    	}
    	
    	public Vector getExit(exit exit){
    		return (Vector) vecExit.clone();
    	}
    }
    Java Code:
    public class exit {
    	
    	public static final int UNDEFINED = 0; 
    	public static final int NORTH = 1; 
    	public static final int SOUTH = 2; 
    	public static final int EAST = 3; 
    	public static final int WEST = 4; 
    	public static final int UP = 5; 
    	public static final int DOWN = 6; 
    	public static final int NORTHEAST = 7; 
    	public static final int NORTHWEST = 8; 
    	public static final int SOUTHEAST = 9; 
    	public static final int SOUTHWEST = 10; 
    	public static final int IN = 11; 
    	public static final int OUT = 12;
    	
    	public static final String[] dirName = {"UNDEFINED", "NORTH", "SOUTH", "EAST", "WEST", "UP", "DOWN", "NORTHEAST", "NORTHWEST", "SOUTHEAST", "SOUTHWEST", "IN", "OUT"};
    	public static final String[] shortDirName = {"NULL", "N", "S", "E", "W", "U", "D", "NE", "NW", "SE", "SW", "I", "O"};
    	
    	room leadsTo = null; 
    	private int direction;
    	private String directionName;
    	private String shortDirectionName;
    	
    	public exit(int direction, room leadsTo){
    		 this.direction = direction;
    		 this.leadsTo = leadsTo;
    		 if (direction <= dirName.length ){
    			 directionName = dirName[direction];
    		 }else if(direction <= shortDirName.length){
    			 shortDirectionName = shortDirName[direction];
    		 }else{
    			 System.out.println("Not a valid Direction.");
    			 throw new ArrayIndexOutOfBoundsException();
    		 }
    	}
    }
    Enter your name:
    Hi
    Character created. "Hi"
    Exception in thread "main" java.lang.NullPointerException
    at room.addExit(room.java:23)
    at init.main(init.java:18)


    Apparently, It doesn't want me to add and element to my vector.

    Bugged Code ↓

    Java Code:
        public void addExit(exit exit){
            vecExit.addElement(exit);
        }
    and

    Java Code:
    town.addExit(new exit(exit.NORTH, magicCircle));

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vectors scare me.

    You haven't initialised your Vector.
    It's still null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    Can you at least tell me how to initialize it then? Cause if I knew, I probably would have done it xD

  4. #4
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    How do I initialize it???

    Java Code:
    vecExit = vecExit.addElement(exit);
    maybe something like that? I have no idea, I'm new to vectors.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Vectors scare me.

    Use the new keyword to create an instance of a class. If not you will allways get a NPE error when you tried to access its instance fields or methods.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vectors scare me.

    Java Code:
    private Vector vecExit;
    That line simply declares a reference variable called 'vecExit'.
    At this point it is referencing nothing...that is it is referencing null.
    This:
    Java Code:
    vecExit = new Vector();
    creates a Vector object in memory, and sets the 'vecExit' variable to point to it.
    Zyril likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    Shoulda thought of that, thanks for making me look like an idiot (Just joking xD, thanks)

  8. #8
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    Hmm, I know someone is just going to refer me to some random tutorial, but to be honest, I don't know where to go from here. How do I find the location the player is currently at?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vectors scare me.

    Store the current location somewhere?
    You could have a very simple Player class, which simply holds the name and the current 'room'.

    Note, classnames should really begin with an upper case. That's the naming standards for Java, so Room and Exit.
    Lowercase first letters are for methods and variables.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    Ok, but I need to do the input first, that way the player can move from place to place, then have the location change.

    So far I have this:

    Java Code:
    		public static void input(){
    			Scanner sc = new Scanner(System.in);
    			command = sc.nextLine();
    			command = command.toLowerCase();
    			for(int i = 0; i < dirName.length; i++){
    				dirName[i] = dirName[i].toLowerCase();
    				if((boolean) (command == dirName[i])){
    					for(int x = 0; x < ; x++){
    						
    					}
    				}
    			}
    		}
    but I can't figure out how to search the vector for the strings, not to mention these are only the long direction names, I even have to add in the short direction names, this is going to take a lot of for loops, which makes me think I'm not doing it optimally anyways.

    Also, I have no idea what the (boolean) is for (I know what boolean means, I just don't know how it applies to the strings. I'm not THAT much of a noob :P), the compiler made me do it even though both dirName and command are strings.
    Last edited by Darkzombies; 11-23-2012 at 08:36 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Vectors scare me.

    You might do better with a Map that maps a String (or enum) which would be the direction, to a value, which would be the exit.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    I don't know what maps are. m(_ _)m

  13. #13
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Vectors scare me.

    If you don't know what Map is then you'd better read this tutorial first: The Map Interface (The Java™ Tutorials > Collections > Interfaces).

  14. #14
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Vectors scare me.

    Well, if I'm going to have to start using maps instead of vectors, I guess I'll have to start a new topic xD, I'll do the tutorial and see if I can apply it to what I already have. If I get stuck you'll probably see me again in the near future. :3

Similar Threads

  1. Need Help with Vectors
    By MadMikeUCF in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 12:07 AM
  2. Need Help Regarding Vectors
    By xHoly in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 06:56 AM
  3. [SOLVED] Vectors
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 05-26-2009, 02:11 PM
  4. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •