?
Solved

Slideshow applet works in appletviewer not in Netscape.

Posted on 1997-12-19
3
Medium Priority
?
377 Views
Last Modified: 2013-11-23

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) { ; }


        }
    }
}
0
Comment
Question by:mattst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1231740
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
 

Author Comment

by:mattst
ID: 1231741

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
 
LVL 5

Accepted Solution

by:
msmolyak earned 600 total points
ID: 1231742
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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question