Board index » jbuilder » getCodeBase() failing with NullPointerException

getCodeBase() failing with NullPointerException


2005-01-11 03:28:24 AM
jbuilder17
It's pretty simple. Create an applet with the wizard. Try to load some
images in the init method (using previously working code straight from the
book) and I fail with a runtime error NullPointerException. What did I do
wrong? Is there a setting missing in the project somewhere?
Mark Bondurant
XXXX@XXXXX.COM
 
 

Re:getCodeBase() failing with NullPointerException

My first project with JBuilder. It's a pretty simple problem, I think.
Create an applet with the wizard. Try to load some images in the init
method (using previously working code straight from the book) and I fail
with a runtime error NullPointerException. What did I do wrong? Is
there a setting missing in the project somewhere? I've isolated
getCodeBase(), and this is definitely the source.
Next problem. The help files show an imgDir directory for images just
off the project root. I created it, stuffed my images in there and it
looks good, but now I've found an "images" directory off the project
object tree. There is no physical directory I can find called "images".
How am I supposed to use this?
Mark Bondurant
XXXX@XXXXX.COM
 

Re:getCodeBase() failing with NullPointerException

Paul Nichols [TeamB] wrote:
Quote
Mark Bondurant wrote:


>My first project with JBuilder. It's a pretty simple problem, I think.
>Create an applet with the wizard. Try to load some images in the init
>method (using previously working code straight from the book) and I fail
>with a runtime error NullPointerException. What did I do wrong? Is
>there a setting missing in the project somewhere? I've isolated
>getCodeBase(), and this is definitely the source.
>
>Next problem. The help files show an imgDir directory for images just
>off the project root. I created it, stuffed my images in there and it
>looks good, but now I've found an "images" directory off the project
>object tree. There is no physical directory I can find called "images".
>How am I supposed to use this?
>
>Mark Bondurantyou
> XXXX@XXXXX.COM


Sounds as though your images are not on the classpath. Are you attempting to
run this within JB or without? If without, are you packaging the applet as
a self-contained jar?

I isolated the getCodeBase() call thusly:
System.out.println("classpath = " + getCodeBase().toString);
with no other code except that created by the wizard and achieved the
same exception. This makes me think it's a setup problem, but I can
find no setting for this. This is so frustrating.
Mark
 

{smallsort}

Re:getCodeBase() failing with NullPointerException

Mark,
It might help if you post the full error message. Are you certain that just
running the code
System.out.println("classpath = " + getCodeBase().toString() );
generates exactly the same error and that the error isn't coming from when
it goes on to run the rest of your code?
You also haven't said how you are trying to run your applet outside of
JBuilder. Are you using Internet Explorer, Netscape or Sun's appletviewer?
If you are using IE or Netscape have you got the plug-in installed and
automatically configured to run in the browser.
Arth
"Mark Bondurant" < XXXX@XXXXX.COM >wrote in message
Quote
Paul Nichols [TeamB] wrote:
>Mark Bondurant wrote:
>
>
>>My first project with JBuilder. It's a pretty simple problem, I think.
>>Create an applet with the wizard. Try to load some images in the init
>>method (using previously working code straight from the book) and I fail
>>with a runtime error NullPointerException. What did I do wrong? Is
>>there a setting missing in the project somewhere? I've isolated
>>getCodeBase(), and this is definitely the source.
>>
>>Next problem. The help files show an imgDir directory for images just
>>off the project root. I created it, stuffed my images in there and it
>>looks good, but now I've found an "images" directory off the project
>>object tree. There is no physical directory I can find called "images".
>>How am I supposed to use this?
>>
>>Mark Bondurantyou XXXX@XXXXX.COM
>
>
>Sounds as though your images are not on the classpath. Are you attempting
>to
>run this within JB or without? If without, are you packaging the applet
>as
>a self-contained jar?
>

I isolated the getCodeBase() call thusly:

System.out.println("classpath = " + getCodeBase().toString);

with no other code except that created by the wizard and achieved the same
exception. This makes me think it's a setup problem, but I can find no
setting for this. This is so frustrating.

Mark

 

Re:getCodeBase() failing with NullPointerException

Arthur Ore wrote:
Quote
It might help if you post the full error message. Are you certain that just
running the code
System.out.println("classpath = " + getCodeBase().toString() );
generates exactly the same error and that the error isn't coming from when
it goes on to run the rest of your code?
Java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:136)
at net.bongo.pancet.Pancet.init(Pancet.java:60)
at net.bongo.pancet.Pancet.main(Pancet.java:100)
exception in thread "main"
the offending line:
60 System.out.println("codebase = " + getCodeBase().toString);
I'm not running it outside of JBuilder. There's nothing complex.
Create an applet with the wizard and then ask for codebase. Build and
then run. It dies with a dialog box from the Java Virtual Machine
Luncher - "Fatal exception occurred. Program will exit."
Quote
You also haven't said how you are trying to run your applet outside of
JBuilder. Are you using Internet Explorer, Netscape or Sun's appletviewer?
If you are using IE or Netscape have you got the plug-in installed and
automatically configured to run in the browser.
No browser. Just JBuilder.
These responses are making me very worried. I had hoped it was just
some sort of set up error.
Mark
 

Re:getCodeBase() failing with NullPointerException

Mark,
This sounds as though it is the way you are running an applet. Just in case
it isn't, what version of JBuilder are you using.
My guess is that although this is an applet, you are running it as
application. I.E., you have a line in it that says
public static void main (String[] args)
The getCodeBase() method assumes that you are running the applet via html,
which is why it is null.
This is probably also the cause of your other error.
If you enter the following code
package test;
import javax.swing.*;
public class Applet1 extends JApplet
{
public void init()
{
System.out.println("classpath = " + getCodeBase().toString() );
}
public static void main (String[] args)
{
Applet1 x = new Applet1();
x.init();
}
}
and run it by right clicking Applet1.java then you will get the error you
report. If however, you run the Applet by right clicking the following html,
then it should work.
<html>
<head>
<title>
HTML Test Page
</title>
</head>
<body>
testy.Applet1 will appear below in a Java enabled browser.<br>
<applet
codebase = "."
code = "test.Applet1.class"
name = "TestApplet"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"
Quote

</applet>
</body>
So basically at the moment, it looks to me as some sort of set up error.
Hence no need to worry ;-)
Arth
Quote

No browser. Just JBuilder.

These responses are making me very worried. I had hoped it was just some
sort of set up error.

Mark

 

Re:getCodeBase() failing with NullPointerException

Arthur Ore wrote:
Quote
Mark,

This sounds as though it is the way you are running an applet. Just in case
it isn't, what version of JBuilder are you using.

My guess is that although this is an applet, you are running it as
application. I.E., you have a line in it that says

public static void main (String[] args)
Yes I do. The wizard put it there.
Quote
The getCodeBase() method assumes that you are running the applet via html,
which is why it is null.

This is probably also the cause of your other error.

If you enter the following code

package test;
import javax.swing.*;
public class Applet1 extends JApplet
{
public void init()
{
System.out.println("classpath = " + getCodeBase().toString() );
}

public static void main (String[] args)
{
Applet1 x = new Applet1();
x.init();
}
}
I'm not using Swing though. I'm using AWT, partly for universal
compatibility over the web, and because that is what I used when I wrote
this thing back in 1997.
Quote
and run it by right clicking Applet1.java then you will get the error you
report. If however, you run the Applet by right clicking the following html,
then it should work.

<html>
<head>
<title>
HTML Test Page
</title>
</head>
<body>
testy.Applet1 will appear below in a Java enabled browser.<br>
<applet
codebase = "."
code = "test.Applet1.class"
name = "TestApplet"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"

</applet>
</body>
So I'm going to run this by looking at the display page included with
the project rather than by using the "run" button, but I'm going to
compile it the normal way. What hell.
The path is the package name followed by the project's root class file.
Quote
So basically at the moment, it looks to me as some sort of set up error.
Hence no need to worry ;-)
Just a plumbing nightmare. I've already destroyed my Linux box trying
to install the Java SDK and JBuilder. Redhat puts stuff who-knows-where
and I'm left with a hunt-the-java-directory day. Find the plugin's,
install them, put the RTE and the compiler in the path - with no
documentation. (gripe, gripe)
So the answer to my second question is probably that all paths for the
project are rooted in the project root. Therefore, when I do manage to
load my .gifs, the path will be imgDir/*.gif.
Thank you very much!
Now for an evening of "Sort Out the Linux Box"
Mark
 

Re:getCodeBase() failing with NullPointerException

Mark Bondurant wrote:
Quote
My first project with JBuilder. It's a pretty simple problem, I think.
Create an applet with the wizard. Try to load some images in the init
method (using previously working code straight from the book) and I fail
with a runtime error NullPointerException. What did I do wrong? Is
there a setting missing in the project somewhere? I've isolated
getCodeBase(), and this is definitely the source.

Next problem. The help files show an imgDir directory for images just
off the project root. I created it, stuffed my images in there and it
looks good, but now I've found an "images" directory off the project
object tree. There is no physical directory I can find called "images".
How am I supposed to use this?

Mark Bondurantyou
XXXX@XXXXX.COM
Sounds as though your images are not on the classpath. Are you attempting to
run this within JB or without? If without, are you packaging the applet as
a self-contained jar?
 

Re:getCodeBase() failing with NullPointerException

Hi Mark,
I think Paul's answered the Linux part of your questions.
The wizard only put the put public static void main (String[] args) because
you either ticked (or left ticked) the run standalone option. Whether you
are using Swing or AWT is imaterial to this problem. As you never pasted any
code I just tried to re-create your error through experience/guesswork.
As to whether Applets are a plumbing nightmare, the answer is both a yes and
no. It depends what you are doing. You need a good book. Some of the things
for Applets you'll need to get to grips with are :-
Do I need a plug-in or not. Increasingly the answer to this is yes as MS are
recommending you use Sun's plug-in rather than their JVM.
Do I need to run the html for the applet through the htmlconverter.
Increasingly the answer to this is no, as long as your clients have opted to
have the plug-in as their default browser JVM.
Do I need to jar up all my code and digitally sign it? You need to read up
on applet security sandbox rules
However, if none of the above are an issue, then applets are a great way to
knock up quick and dirty test programmes. If you need to get to grips with
digitally signing an Applet, I think Paul Nichols has a good link on his
web-site. www.computer-logic.net/javafaq/ However, remembering back a
few years, the way you sign an Applet that targets MS's default JVM is
different. You'll need to search Sun's web site and google for further
information on this.
Also you might want to have a look at Rich Wilkman's web site.
www.formlessvoid.com/jc/index
Arth
"Mark Bondurant" < XXXX@XXXXX.COM >wrote in message
Quote
Arthur Ore wrote:
>Mark,
>
>This sounds as though it is the way you are running an applet. Just in
>case it isn't, what version of JBuilder are you using.
>
>My guess is that although this is an applet, you are running it as
>application. I.E., you have a line in it that says
>
>public static void main (String[] args)

Yes I do. The wizard put it there.

>The getCodeBase() method assumes that you are running the applet via
>html, which is why it is null.
>
>This is probably also the cause of your other error.
>
>If you enter the following code
>
>package test;
>import javax.swing.*;
>public class Applet1 extends JApplet
>{
>public void init()
>{
>System.out.println("classpath = " + getCodeBase().toString() );
>}
>
>public static void main (String[] args)
>{
>Applet1 x = new Applet1();
>x.init();
>}
>}

I'm not using Swing though. I'm using AWT, partly for universal
compatibility over the web, and because that is what I used when I wrote
this thing back in 1997.

>and run it by right clicking Applet1.java then you will get the error you
>report. If however, you run the Applet by right clicking the following
>html, then it should work.
>
><html>
><head>
><title>
>HTML Test Page
></title>
></head>
><body>
>testy.Applet1 will appear below in a Java enabled browser.<br>
><applet
>codebase = "."
>code = "test.Applet1.class"
>name = "TestApplet"
>width = "400"
>height = "300"
>hspace = "0"
>vspace = "0"
>align = "middle"
>
></applet>
></body>

So I'm going to run this by looking at the display page included with the
project rather than by using the "run" button, but I'm going to compile it
the normal way. What hell.

The path is the package name followed by the project's root class file.

>So basically at the moment, it looks to me as some sort of set up error.
>Hence no need to worry ;-)

Just a plumbing nightmare. I've already destroyed my Linux box trying to
install the Java SDK and JBuilder. Redhat puts stuff who-knows-where and
I'm left with a hunt-the-java-directory day. Find the plugin's, install
them, put the RTE and the compiler in the path - with no documentation.
(gripe, gripe)

So the answer to my second question is probably that all paths for the
project are rooted in the project root. Therefore, when I do manage to
load my .gifs, the path will be imgDir/*.gif.

Thank you very much!

Now for an evening of "Sort Out the Linux Box"

Mark
 

Re:getCodeBase() failing with NullPointerException

Hmmmm . . . good advice. I will try to digest it, especially the Linux
stuff today.
Thanks!
Mark
 

Re:getCodeBase() failing with NullPointerException

"Mark Bondurant" < XXXX@XXXXX.COM >wrote in message
Quote
Arthur Ore wrote:
>Mark,
>
Just a plumbing nightmare. I've already destroyed my Linux box trying
to install the Java SDK and JBuilder. Redhat puts stuff who-knows-where
and I'm left with a hunt-the-java-directory day. Find the plugin's,
install them, put the RTE and the compiler in the path - with no
documentation. (gripe, gripe)

FYI,
For Linux, download and install the Javasoft JVM. It will install in the
/usr/java directory, by default.
To set the path, look under /usr/local and /usr/share for a symbolic link.
Delete it, and redo the sym link to /usr/java/<j2sdk_version>
I haven't used RHat in some time. We have some Rhat boxes at work, but I am
mainly coding on Solaris boxes and Suse Linux boxes. Not sure where the sym
links are on Rhat, but usually under /usr/share or /usr/lib.
Of course, you can manually set it using setenv
JAVA_HOME=/usr/java/<j2sdk_version>export