Results 1 to 8 of 8
Thread: Try/Catch Class Scope Issue
- 01-11-2011, 03:25 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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:
Then this is essentially how I am calling it from another class: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"); } }
I am getting a NullPointerException on the "recipe=" line and can only assume the scope of the props variable is getting truncated.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");
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!
-
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.
- 01-11-2011, 03:43 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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!
-
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.
- 01-11-2011, 03:58 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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.
-
- 01-11-2011, 09:02 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-11-2011, 02:47 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
I did finally come across a solution.
What I was doing was declaring the new class in the first class by using this statement:
Thus calling the constructor of the new class immediately, so the variable was actually null. Just learning things as I go, thx guys.Java Code:Newclass nc = new Newclass(this);
Similar Threads
-
Class cast issue
By AedonetLIRA in forum New To JavaReplies: 5Last Post: 11-19-2010, 04:34 PM -
Calendar class issue
By OlegKo in forum New To JavaReplies: 3Last Post: 10-24-2010, 06:36 PM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
Class Scanner looping issue
By Stev0 in forum New To JavaReplies: 1Last Post: 05-25-2008, 06:53 PM -
Try Catch issue
By curtis_fraser in forum Advanced JavaReplies: 2Last Post: 12-13-2007, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks