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

program works but wont display in browser

0 views
Skip to first unread message

andre...@alltel.net

unread,
Feb 27, 2006, 9:42:44 PM2/27/06
to
I wrote a small picture viewer in java and tha program runs but after I put
it online I get a null pointer exception. Not sure why since it compiles and
runs fine otherwise.
Can someone please point me in the right direction.

Thank You,
Andy

//Web Page
//By: Andrew Titus
//Date 2-26

import javax.swing.JApplet;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.*;


public class WebApplet extends JApplet
{

private JButton previous,next;
private JPanel buttonPanel;
Image coolImage[] = new Image[5];
Font title,documentText;
int i = 0;

String[] names = {"StBasials1.jpg", "museum9.jpg", "Red_Square9.jpg",
"StBasials8.jpg",
"Russian_Soilders_in_Formation.jpg" };


public void init()
{
previous = new JButton("Previous");
next = new JButton("Next");

for(int j=0; j<names.length;j++) {
coolImage[j] = getImage(getCodeBase(),names[j] );
}


ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);

buttonPanel = new JPanel();

buttonPanel.add(previous);
buttonPanel.add(next);

add(buttonPanel);

} // end init()



public void paint(Graphics page)
{
setBackground(Color.black);


title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText = new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );

//set color for text
page.setColor(Color.white);
page.setFont(title);


page.setFont(documentText);


page.drawImage(coolImage[i],30,60,this);

} //end paint()

private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(event.getSource() == next){
i++;
repaint();
}
if(event.getSource() == previous) {
i--;
repaint();
}
}

}


} //end webApplet

hiwa

unread,
Feb 28, 2006, 12:41:25 AM2/28/06
to
Using JApplet is a good idea, but it is
a little bit different from good old
java.applet.Applet. Read the API
documentation of JApplet class and
the tutorial linked from there.
----------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class WebApplet extends JApplet{
private JButton previous,next;
private JPanel buttonPanel;

Font title, documentText;
int i = 0;
Container con;
ImagePanel ip;
String[] names
= {"Pic1.jpg", "Pic2.jpg", "Pic3.jpg", "Pic4.jpg", "Pic5.jpg" };
Image[] coolImage;
ImageIcon[] iia;

public void init(){
con = getContentPane();

// you should use MediaTracker, or, use ImageIcon that uses
// MediaTracker internally
coolImage = new Image[names.length];
iia = new ImageIcon[names.length];
for (int i = 0; i < names.length; ++i){
iia[i] = new ImageIcon(getImage(getCodeBase(),names[i]));
coolImage[i] = iia[i].getImage();
}

previous = new JButton("Previous");
next = new JButton("Next");

ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);

ip = new ImagePanel();

buttonPanel = new JPanel();
buttonPanel.add(previous);
buttonPanel.add(next);
add(buttonPanel);

con.add(ip, BorderLayout.CENTER);
con.add(buttonPanel, BorderLayout.SOUTH);
}

class ImagePanel extends JPanel{

public ImagePanel(){
setBackground(Color.black);
}

public void paintComponent(Graphics g){
super.paintComponent(g);

title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText
= new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );

g.setColor(Color.white);
g.setFont(title);

g.setFont(documentText);

g.drawImage(coolImage[i],30,60,this);
}
} // class ImagePanel

private class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event){
if(event.getSource() == next){
i++;

if (i >= names.length){
i = 0;
}
}
else if(event.getSource() == previous){
i--;
if (i < 0){
i = names.length - 1;
}
}
ip.repaint();
}
}
} // class webApplet
------------------------------------------------------------------

hiwa

unread,
Feb 28, 2006, 12:48:32 AM2/28/06
to
Wow, delete this line:
> add(buttonPanel);

andre...@alltel.net

unread,
Feb 28, 2006, 1:30:30 AM2/28/06
to
I tried to go there at suns java site and not sure what I am looking for.

Andy

hiwa

unread,
Feb 28, 2006, 1:55:14 AM2/28/06
to
> I tried to go there at suns java site and not sure what I am looking for.
>
> Andy
Go to this page:
http://java.sun.com/j2se/1.5.0/download.jsp
and click the link:
J2SE 5.0 Documentation [Download]

Paul Hamaker

unread,
Feb 28, 2006, 3:09:53 AM2/28/06
to

Oliver Wong

unread,
Feb 28, 2006, 1:28:00 PM2/28/06
to

<andre...@alltel.net> wrote in message
news:zb2dnWMG9qY4JZ7Z...@giganews.com...

>I wrote a small picture viewer in java and tha program runs but after I put
> it online I get a null pointer exception. Not sure why since it compiles
> and
> runs fine otherwise.
> Can someone please point me in the right direction.
>
> Thank You,
> Andy
>
[code snipped]

What's the exeception, and what line does the exception refer to?

- Oliver

andre...@alltel.net

unread,
Feb 28, 2006, 2:17:56 PM2/28/06
to
It works right but buttons dont display until I move mouse over them and the
main problem Iam having is when I upload WebApplet.class to server and try
and display in web page I get a

exception: java.lang.nullPointerException error and the applet will not
load. Not sure why this is happening.

Thank You,
Andy

//Web Page
//By: Andrew Titus
//Date 2-26

//import java.applet.Applet;


import javax.swing.JApplet;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.*;

import javax.swing.JButton;

Oliver Wong

unread,
Feb 28, 2006, 3:30:32 PM2/28/06
to

<andre...@alltel.net> wrote in message
news:PbSdnQUIhcJ...@giganews.com...

> It works right but buttons dont display until I move mouse over them and
> the
> main problem Iam having is when I upload WebApplet.class to server and try
> and display in web page I get a
>
> exception: java.lang.nullPointerException error and the applet will not
> load. Not sure why this is happening.

Usually exceptions are accompanied with a stack trace which have line
numbers. If you could post the line numbers, it'd be easier to determine
where the null pointer exception is occurring. If you know how to use the
applet viewer, you might want to use that instead of a full fledge web
browser.

Otherwise, look in your browser for a feature to display the "Java
Console"; that should show a text box with the stacktrace present.

- Oliver

andre...@alltel.net

unread,
Feb 28, 2006, 3:49:16 PM2/28/06
to
When I run the program in viewer all works till end of array then if I push
the button for next pix I get

ÏException in thread "AWT-EventQueue-1"
java.lang.ArrayIndexOutOfBoundsException: 5
ÏÏ§Ï at WebApplet.paint(WebApplet.java:86)
ÏÏ§Ï at javax.swing.JApplet.update(JApplet.java:148)
ÏÏ§Ï at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
ÏÏ§Ï at sun.awt.RepaintArea.paint(RepaintArea.java:216)
ÏÏ§Ï at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
ÏÏ§Ï at java.awt.Component.dispatchEventImpl(Component.java:4031)
ÏÏ§Ï at java.awt.Container.dispatchEventImpl(Container.java:2024)
ÏÏ§Ï at java.awt.Component.dispatchEvent(Component.java:3803)
ÏÏ§Ï at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
ÏÏ§Ï at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
ÏϧÏ
When I upload applet to internet to view in browser at the bottom of browser
it gives

exception: java.lang.nullPointerException error

THis is all it gives me down at bottom of browser

when I run in the applet viewer it lets me scroll through pictures but not
sure why out of bounds error and the other exception.

Thank You,
Andy

andre...@alltel.net

unread,
Feb 28, 2006, 3:54:54 PM2/28/06
to
Here is the error with browser.

Andy

load: class WebApp.class not found.
java.lang.ClassNotFoundException: WebApp.class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 10 more
Exception in thread "Thread-4" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-WebApp.class"
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

andre...@alltel.net

unread,
Feb 28, 2006, 3:59:01 PM2/28/06
to
Oki found one big error I had class named WebApp.class instead of
WebApplet.class but now get the following error:

java.lang.NoClassDefFoundError: WebApplet$ButtonListener
at WebApplet.init(WebApplet.java:43)


at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Exception in thread "thread applet-WebApplet.class"


java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Andy

Oliver Wong

unread,
Feb 28, 2006, 4:14:38 PM2/28/06
to

<andre...@alltel.net> wrote in message
news:ZdWdndSUC8k...@giganews.com...

What did you do to fix the error? You didn't simply rename the file to
"WebApp.class", did you? The name of the class file must match the name of
the public class declared within that file. Your file has a line which reads
"public class WebApplet extends JApplet", so the public class is called
"WebApplet", and the class file must be named "WebApplet.class".

- Oliver

andre...@alltel.net

unread,
Feb 28, 2006, 4:21:52 PM2/28/06
to
Yes I renamed in HTML file WebApplet.class. I also realized that I needed to
upload button listener class and another class it created when compiled. It
is uploaded and works now.
I have some things to do with it still like figure out how to get button to
stop after end of pictures and get buttons to display without needing mouse
overtop of them to display.
Any advise?

Hey I want to thank you for your time and help. If you did not point that I
can view error with browser in IE 6 I would have never found the mistakes.

Andy

andre...@alltel.net

unread,
Feb 28, 2006, 4:23:15 PM2/28/06
to
Here is page like if you want to see it.

Andy

http://home.alltel.net/andrewtitus/

Oliver Wong

unread,
Feb 28, 2006, 4:43:19 PM2/28/06
to

<andre...@alltel.net> wrote in message
news:0-qdnShkbOtvI5nZ...@giganews.com...

> I have some things to do with it still like figure out how to get button
> to
> stop after end of pictures and get buttons to display without needing
> mouse
> overtop of them to display.
> Any advise?

Might have something to do with threading; not sure. See this article:
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html

It says you shouldn't directly edit the GUI (e.g. creating buttons and
adding them to panels, etc.) in the init() method, but that you should use
Swing's invokeAndWait() to create the buttons in the EDT.

>
> Hey I want to thank you for your time and help. If you did not point that
> I
> can view error with browser in IE 6 I would have never found the mistakes.

Your welcome.

- Oliver

hiwa

unread,
Feb 28, 2006, 10:55:06 PM2/28/06
to
It is incredible you haven't learn nothing from my code!

The most important one is:
public void paintComponent(Graphics g){ // not overriding paint() for
Swing component

The paint() method of Swing component should
call atleast three paintXxxx() methods, one of
which is paintComponent(). So you should never
override the paint() method in your custom class.

And make a custom panel and override its
paintComponent() method. Don't do it
for you JApplet class. Because JApplet
is, like JFrame, a top level composite
container. You should add components
on its content pane.

And, this is the array index part of my code:

0 new messages