Results 1 to 8 of 8
  1. #1
    bnshrdr is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Try/Catch Class Scope Issue

    I typically don't develop in java but the program I want to develop a plugin for gives me no choice :P

    The problem I am having arises from me trying to use a variable from another class that was assigned in a Try/Catch block.

    The code for the initial class is below:
    Java Code:
    public class RepairMe extends JavaPlugin
    {
    	//Listener Classes
    	private final playerListener playerL = new playerListener(this);
    	//Database variables
    	public IniLib props;
    	public InputStream in;
    	
    	public RepairMe(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader)
    	{
    		super(pluginLoader, instance, desc, plugin, cLoader);
    		registerEventHooks();
    	}
    	
    	private void registerEventHooks() 
    	{
    		getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerL, Priority.Normal, this);
    	}
    	
    	public void onDisable()
    	{
    		System.out.println("RepairMe v1.0 Disabled");
    	}
    	
    	public void onEnable()
    	{
    		//Initialization
    		 props = new IniLib();
    		 in = null;
    		 
    		//Try to load RepairMe.ini
    		try {
    			in = new BufferedInputStream(new FileInputStream("RepairMe.ini"));
    			props.load(in);
    		} catch (FileNotFoundException e) {
    			System.out.println("Error finding RepairMe.ini");
    			getServer().getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin("RepairMe"));
    			return;
    		} catch (IOException e) {
    			System.out.println("Error loading RepairMe.ini");
    			getServer().getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin("RepairMe"));
    			return;
    		}		
    		System.out.println("RepairMe v1.0 Enabled");
    	}	
    }
    Then this is essentially how I am calling it from another class:
    Java Code:
    public class playerListener extends PlayerListener 
    {
        //private final RepairMe plugin;
        IniLib props;
        
        public playerListener(RepairMe instance)
        {
            //plugin = instance;
            props = instance.props;
        }
    
        ....SNIP.....
    	@Override
    	public void onPlayerCommand(PlayerChatEvent event)
    	{
            ....SNIP....
    				[B]recipe = props.getProperty(item.getType().name(), "recipe");[/B]
    				if (recipe != null)
    					player.sendMessage(recipe);
    				else
    					player.sendMessage("No exisiting recipe");
    I am getting a NullPointerException on the "recipe=" line and can only assume the scope of the props variable is getting truncated.

    Does anyone see any blatantly obvious mistake I've made. I'm just not a Java programmer and HATE exceptions haha. Guess I'm anti OO haha.

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bnshrdr View Post
    I am getting a NullPointerException on the "recipe=" line and can only assume the scope of the props variable is getting truncated.
    What do you mean by "scope is getting truncated"? As I've never heard this before and it makes no sense. Usually a scope problem will cause a compiler error not a run-time exception (unless you are shadowing the variable of interest without realizing it). I don't think that this is a scope issue but rather you are trying to call a method or get a field from a variable that is null, pure and simple. Walk back from the exception and see where you supposedly initiate the null variable.

  3. #3
    bnshrdr is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Well I don't know how it is getting to be null. If I call the same method right after setting props in the try/catch block, it works successfully.

    So since it is obviously not null in the RepairMe class, I fear that this really narrows it down to a scope issue since it is read as null from another class.

    If you could look at how I declared the variables in the RepairMe class, am I accessing them in a legal way outside of the class? Again, I am no java guru, but have enough programming experience to kind of shove me along.

    Thanks for taking a look!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    And are you sure which variable is null on that line? Are you sure that it's not the item variable or its type? Have you done System.out.println's to check this? It looks like you need to do some serious debugging here. Luck.

  5. #5
    bnshrdr is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I have checked the return of item.getType().name(), which is not null. But props is returning null.

    What it comes down to is I have no idea why props is null, even though I am passing the class instance to the new class.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bnshrdr View Post
    I have checked the return of item.getType().name(), which is not null. But props is returning null.

    What it comes down to is I have no idea why props is null, even though I am passing the class instance to the new class.
    Frankly neither do I. I think that the solution lies in doing some aggressive debugging either with a debugger or with the poor-man's debugger (println statements). Luck.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Have a look and see if you have either redeclared a props variable in that method, thus hiding the attribute, or if you are reinstantiating props and it's failing.

    The code you have shown us is not your problem. Your problem is in the code you are not showing us.

  8. #8
    bnshrdr is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    I did finally come across a solution.

    What I was doing was declaring the new class in the first class by using this statement:
    Java Code:
    Newclass nc = new Newclass(this);
    Thus calling the constructor of the new class immediately, so the variable was actually null. Just learning things as I go, thx guys.

Similar Threads

  1. Class cast issue
    By AedonetLIRA in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 04:34 PM
  2. Calendar class issue
    By OlegKo in forum New To Java
    Replies: 3
    Last Post: 10-24-2010, 06:36 PM
  3. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  4. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 06:53 PM
  5. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2007, 11:04 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
  •