Solved

Slideshow applet works in appletviewer not in Netscape.

Posted on 1997-12-19
3
370 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
  • 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 300 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 86
network + 7 73
topping3 challenge 14 52
Running Jira on Raspberry PI 2? 3 139
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now