Results 1 to 5 of 5
  1. #1
    nuffsaidM8 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Getting variables from another class, then replacing them

    I'm working with an api. The type of the variable I am using is Location. I have three classes. My main class has defined this variable:

    Java Code:
    public static Location spawn;
    In my other calsses I try to get this variable like this:

    Java Code:
    Location changeSpawn = EasySpawn.spawn;
    EasySpawn being the main class name. Later I try to set the new value that I have assigned to the spawn variable in the main class like this:

    Java Code:
    EasySpawn.spawn = changeSpawn;
    But when I try to access the spawn Location in the third class, after I change the spawn location, it tells me that the spawn location is null. I access the variable the same way as in the second calss. Why is this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Getting variables from another class, then replacing them

    It could be a case of shadowing but its hard to tell. If you submit a Short, Self Contained, Correct Example it will be must easier to assist you.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    nuffsaidM8 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Getting variables from another class, then replacing them

    My classes are very small because that is all I need for what I am trying to do.

    Main:
    Java Code:
    package com.nuffsaidM8.easySpawn;
    
    import org.bukkit.Location;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class EasySpawn extends JavaPlugin implements Listener{
    	
    	public static Location spawn; //original declaration of the variable
    	
    	public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this); //don't worry about this, this line works fine.
            this.getCommand("spawn").setExecutor(new Spawn()); //runs when command "Spawn" is used
            this.getCommand("worldspawnset").setExecutor(new WorldSpawnSet()); //runs when command"woldspawnset" is used
        }
        
        public void onDisable() {
        	
        }
    }
    Class for worldspawnset command:

    Java Code:
    package com.nuffsaidM8.easySpawn;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class WorldSpawnSet implements CommandExecutor{
    
    	Location setSpawn = EasySpawn.spawn;  //I get the variable from the main class, EasySpawn
    	
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
    			String[] args) {
    		if(!(sender instanceof Player)){
    			sender.sendMessage("You must be a player to use this command!");
    			return false;
    		};
    		Player player = (Player) sender;
    		if(player.hasPermission("admin.world.spawn.set")){
    			setSpawn = player.getLocation(); //sets the location of the variable declared above to where the command is run from
    			EasySpawn.spawn = setSpawn; //sets the location of the new variable to the variable declared in the main class
    			player.sendMessage(ChatColor.YELLOW + "World Spawn Location Set!");
    		} else {
    			player.sendMessage("You don't have permission to use this command.");
    		};
    		return true;
    	};
    	
    }
    code for my spawn command:

    Java Code:
    package com.nuffsaidM8.easySpawn;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Spawn implements CommandExecutor{
    	
    	
    	Location getSpawn = EasySpawn.spawn;
    	
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
    			String[] args) {
    		if(!(sender instanceof Player)){
    			sender.sendMessage("You must be a player to use this command!");
    			return false;
    		};
    		Player player = (Player) sender;
    		if(getSpawn == null){
    			player.sendMessage(ChatColor.RED + "No Spawn Location Has Been Set!");
    			return false;
    		};
    		player.teleport(getSpawn); //moves the player to whatever the value of the variable spawn is
    		player.sendMessage(ChatColor.YELLOW + "Teleported to Spawn!");
    		return true;
    	};
    
    };
    Is it possible that the variable is sent to the spawn class while it is still null (when the program first runs and the variable doesn't have a value) and is never updated?
    Last edited by nuffsaidM8; 07-22-2015 at 10:45 PM.

  4. #4
    nuffsaidM8 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Getting variables from another class, then replacing them

    Quote Originally Posted by jim829 View Post
    It could be a case of shadowing but its hard to tell. If you submit a Short, Self Contained, Correct Example it will be must easier to assist you.

    Regards,
    Jim
    What do you mean by shadowing?

    Also, see my post above for the code.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Getting variables from another class, then replacing them

    It looks like you create an new instance of Spawn before the value in EasySpawn is set. I recommend you sprinkle some print statements through out your classes before and after you create instances to check on the status of your variables.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Variables from another class won't assign to main class
    By erthbound0 in forum New To Java
    Replies: 3
    Last Post: 12-15-2011, 10:07 PM
  2. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 01:27 AM
  3. getting class to recognize variables from another class
    By shadycharacter in forum New To Java
    Replies: 1
    Last Post: 04-26-2010, 10:14 PM
  4. Accessing Variables From a Different Class?
    By Sterling in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 05:01 PM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 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
  •