------------------------ResourcesManager.java---------------------
package com.dev.test;
import java.util.ResourceBundle;
public class ResourcesManager {
ResourceBundle resourceBundle = ResourceBundle.getBundle("test");
public String stringFetcher(String key) {
System.out.print(key);
return resourceBundle.getString(key);
}
}
---------------------End code----------------------------
and the second class
-------------------Editor.java--------------------------
package com.dev.test;
import java.util.ResourceBundle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Editor {
Shell shell;
Display display;
ResourcesManager resources;
//ResourceBundle resources2 = ResourceBundle.getBundle("test");
public void run() {
display = new Display();
Editor editor = new Editor();
shell = editor.open(display);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private Shell open(Display display) {
createShell(display);
shell.open();
return shell;
}
private void createShell(Display display) {
shell = new Shell(display);
shell.setText(resources.stringFetcher("Title")); // Throws
nullpointerexcepion
//shell.setText(resources2.getString("Title")); // no exceptions
shell.setSize(600, 800);
}
}
----------------------------End code------------------------------
If I call the method ResourcesManager.stringFetcher() from Editor class,
I will only get NullPointerException, but if I call ResourceBundle
directly from Editor class, it just works as expected. Can someone pls
explain this behavior?
This is never initialized to anything so is null. This leads to
NullPointerException when you try to call a method on it.
Try to replace the line with
ResourcesManager resources = new ResourcesManager();
Cheers,
Bent D
--
Bent Dalager - b...@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Thank you for your fast reply. You're right, I forgot to initialized
'resource' variable. I feel a little stupid now, asking stupid question
like that. :P
Bent got your NPE straightened out. I'm a little puzzled about the 'run()'
method of 'Editor' creating a brand new instance of 'Editor' and operating on
that instead of its own members.
--
Lew
>NullPointerException
Others will give you specific help. Here is what do to in general.
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
--
Roedy Green Canadian Mind Products
http://mindprod.com
"If everyone lived the way people do in Vancouver,
we would need three more entire planets to support us."
~ Guy Dauncey (born: 1948 age: 61)