Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can someone pls explain why ResourceBundle throw nullpointerexception

358 views
Skip to first unread message

Canned

unread,
Feb 25, 2009, 6:17:55 PM2/25/09
to
Hi,
I have a newbie question, I hope you don't mind explaining this problem
to me.
I wrote a class that only have 1 method, like this one

------------------------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?

Bent C Dalager

unread,
Feb 25, 2009, 6:34:42 PM2/25/09
to
On 2009-02-25, Canned <us...@domain.invalid> wrote:
>
> public class Editor {
> Shell shell;
> Display display;
>
> ResourcesManager resources;

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

Canned

unread,
Feb 25, 2009, 6:49:18 PM2/25/09
to
Bent C Dalager schreef:

> On 2009-02-25, Canned <us...@domain.invalid> wrote:
>> public class Editor {
>> Shell shell;
>> Display display;
>>
>> ResourcesManager resources;
>
> 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

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

Lew

unread,
Feb 26, 2009, 12:04:09 AM2/26/09
to

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

Canned

unread,
Feb 26, 2009, 5:02:40 PM2/26/09
to
Lew schreef:
Now that you mention it, I rewrite it like this: shell = open();. I
found an examples from internet and trying to write my own based on
that:
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTTextEditorDemo.htm

Roedy Green

unread,
Feb 26, 2009, 10:56:09 PM2/26/09
to
On Thu, 26 Feb 2009 00:17:55 +0100, Canned <us...@domain.invalid>
wrote, quoted or indirectly quoted someone who said :

>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)

0 new messages