Slideshow applet works in appletviewer not in Netscape.


This applet shows a slide show of images in appletviewer and in
Hotjava but does not work in Netscape (4.03) or in
Internet Explorer (3.02). The error messages are:

In Netscape:
Applet SlideShow java.lang.NullPointerException.

In IExplorer:
exception.ms.applet.AppletSecurityException: security. Couldn't
connect to with origin from file.

I have spent hours trying to solve this and can't. It seems to
me strange that I should get a Null Pointer Exception in
Netscape but not in Appletviewer. For the life of me I can't
work out what object is causing this cos they all *seem* to
point at something.

Here is SlideShow.java (beneath this is my posting from
yesterday).

Thanks -- matthew.

------------------------------------------------------------
import java.applet.Applet;
import java.awt.*;

public class SlideShow extends Applet {

    public static final int NUM_IMAGES  = 10;
    public static final int WAIT_MILLIS = 1500;

    Image image[] = new Image[NUM_IMAGES];

    String name[] = { "allen.jpg", "bergman.jpg", "bogart.jpg", "eastwood.jpg",
                      "hitchcock.jpg", "hoffman.jpg", "monroe.jpg",
"sellers.jpg",
                      "spielberg.jpg", "welles.jpg" };

    public void init() {

        for (int i = 0; i < NUM_IMAGES; i++)  {
            image[i] = getImage(getDocumentBase(), name[i]);
        }
    }

    // The background rectangle fills the frame so we
    // don't need to clear the applet on repaints.
    // Just call the paint method.
    public void update(Graphics g) {
        paint(g);
    }


    // Paint a large red rectangle if there are any errors
    // loading the images or do slide show.

    public void paint(Graphics g) {

        int wSize = 340;
        int hSize = 390;
        int wBorder = 20;
        int hBorder = 0;

        Color bg = new Color(0,0,0);
        g.setColor(bg);
        g.fillRect(0, 0, wSize, hSize);

        for (int i = 0; i < NUM_IMAGES; i++)  {
            g.fillRect(0, 0, wSize, hSize);
            hBorder = image[i].getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(image[i], wBorder, hBorder, this);

            try { Thread.sleep(WAIT_MILLIS); }
            catch (InterruptedException e) { System.out.println("Wait error.");
}

        }  // End for

    }  // End paint
}  // End SlideShow class


and yesterdays posting is:
-----------------------------------------------------------------

Hi --: Please help:

This java applet - SlideShow.java (compiled with JDK1.0) - works fine in
appletviewer but not in Netscape 4.03. The error is
java.lang.NullPointerException. All it does is preload 10 jpegs and display them
with a second delay inbetween.

Thanks,

..matthew          (Replies CCed by email would be helpful).

-- SlideShow.java --

import java.applet.Applet;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;

public class SlideShow extends Applet implements Runnable {

    Image al, be, bo, ea, hi, ho, mo, se, sp, we;
    MediaTracker tracker;
    Thread slide;
    int index = 0;
    int numPics = 10;
    int waitMillis = 1000;

    public void init() {
        tracker = new MediaTracker(this);

        al = getImage(getDocumentBase(), "allen.jpg");
        be = getImage(getDocumentBase(), "bergman.jpg");
        bo = getImage(getDocumentBase(), "bogart.jpg");
        ea = getImage(getDocumentBase(), "eastwood.jpg");
        hi = getImage(getDocumentBase(), "hitchcock.jpg");
        ho = getImage(getDocumentBase(), "hoffman.jpg");
        mo = getImage(getDocumentBase(), "monroe.jpg");
        se = getImage(getDocumentBase(), "sellers.jpg");
        sp = getImage(getDocumentBase(), "spielberg.jpg");
        we = getImage(getDocumentBase(), "welles.jpg");

        tracker.addImage(al, 0);
        tracker.addImage(be, 0);
        tracker.addImage(bo, 0);
        tracker.addImage(ea, 0);
        tracker.addImage(hi, 0);
        tracker.addImage(ho, 0);
        tracker.addImage(mo, 0);
        tracker.addImage(se, 0);
        tracker.addImage(sp, 0);
        tracker.addImage(we, 0);
    }

    // Start the animation thread.
    public void start() {
        slide = new Thread(this);
        slide.start();
    }

    // Stop the animation thread.
    public void stop() {
        slide.stop();
        slide = null;
    }

    // Run the slide show thread.
    // Wait for all of the pics to finish loading.
    public void run() {
        try {
            tracker.waitForAll();
        }
        catch (InterruptedException e) {
            return;
        }

        Thread me = Thread.currentThread();
        while (slide == me) {
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                break;
            }

            synchronized (this) {
                index++;
                if (index == numPics) {
                    index = 0;
                }
            }
            repaint();
        }
    }


    // The background fills the frame so we
    // don't need to clear the applet on repaints.
    // Just call the paint method.
    public void update(Graphics g) {
        paint(g);
    }


    // Paint a red rectangle if there are any errors
    // loading the images.

    public void paint(Graphics g) {

        if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
            g.setColor(Color.red);
            g.fillRect(0, 0, 40, 40);
            return;
        }

        if (tracker.statusID(0, false) == MediaTracker.COMPLETE) {

            int wSize = 340;
            int hSize = 390;
            int wBorder = 20;
            int hBorder = 0;

            Color bg = new Color(63, 97, 158);  // Background of web page

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = al.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(al, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = be.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(be, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = bo.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(bo, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = ea.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(ea, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = hi.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(hi, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = ho.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(ho, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = mo.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(mo, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = se.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(se, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = sp.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(sp, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }

            g.setColor(bg);
            g.fillRect(0, 0, wSize, hSize);
            hBorder = we.getHeight(this);
            hBorder = (hSize - hBorder) / 2;
            g.drawImage(we, wBorder, hBorder, this);
            try { Thread.sleep(waitMillis); }
            catch (InterruptedException e) { ; }


        }
    }
}
mattstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

msmolyakCommented:
I've experienced some erratic problems with loading images in Netscape 4.03. I was also getting a NullPointerException though it would happen only sometimes (also with JDK 1.0.2 applet). And I also could not reproduce that problem in appletviewer. A few suggestions:
- try reloading applet after it failed;
- try openning the java console before running the applet and typing 9 there to change the debug messaging level. That should produce more diagnostics.
- try getting the document base from the classpath as well as from the web server (that is if you are doing it one way, try another);
- try upgrading to Netscape 4.04.

Looks like the Explorer error has nothing to do with Netscape's error.
0
mattstAuthor Commented:

Thanks for your help. You were right it is a 1.0.2 applet.

It looks like the Null Pointer error is happening because the images fail to load, i.e:
# Security Exception: class from local disk trying to access url:file:/E|/Jsrc/SlideShow/allen.jpg

I suspect that:

>- try getting the document base from the classpath as well
>as from the web server (that is if you are doing it one way, try
>another);

this may sort it out but do not know how to accomplish it. Please  advise.

Thanks,

..matthew

0
msmolyakCommented:
My idea was to serve Java classes from a Web server rather than from CLASSPATH. Waht that means is that you place you Java class files on the machine where the Web server is running in underneath the appropriate directory for Java applets. You place your HTML file in that same directory and make sure that either the classes are directly accessable from that directory or you have a CODEBASE tag in you HTML which points to where the classes can be found.  The image files will also be on the server side.

After that start your browser, connect to that Web site and run the applet (if you are running browser on teh same machine make sure you disable the classpath otherwise Netscape will continie loading the classes from the classpath rather than from the Web server).

Let me know if you need more help.

Michael
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.