# Java Runtime Error

## Recommended Posts

Okay, I'm in school, and my teacher won't help me. I'm getting run-time errors in my Java program, and I don't know why.
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Flora extends Frame implements ActionListener
{
public static void main(String[] args)
{
Flora window = new Flora();
window.setTitle("Flora City Stickers");
window.setSize(450,250);
window.setVisible(true);
}

public Flora()
{
setBackground(Color.magenta);
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e){}
}
java.lang.ClassCastException: Flora at sun.applet.AppletPanel.createApplet(AppletPanel.java:710) at sun.applet.AppletPanel.runLoader(AppletPanel.java:639) at sun.applet.AppletPanel.run(AppletPanel.java:319) at java.lang.Thread.run(Thread.java:595) The frame won't initialize and I have no clue why. This code is exactly from the book, so if the book has faulty code, well, I'm in trouble. [Edited by - Drunken_Monkey on April 24, 2006 8:04:58 AM]

##### Share on other sites
dave    2187
Hey bud,

Is that a paste of the whole thing. It appears to me, when i formatted it, that there are places where parenthesis and scope brackets are out of place.

See if this compiles:
import java.io.*;import java.awt.*;import java.awt.event.*;public class Flora extends Frame implements ActionListener{   public static void main(String[] args)   {      Flora window = new Flora();      window.setTitle("Flora City Stickers");      window.setSize(450,250);      window.setVisible(true);   }   public Flora()   {      setBackground(Color.magenta);      addWindowListener( new WindowAdapter() );   }   public void windowClosing(WindowEvent e)   {      System.exit(0);   }   public void actionPerformed(ActionEvent e)   {   }};

Hope that helps,

Dave

##### Share on other sites
Antheus    2409
Quote:
 Original post by DaveHey bud,Is that a paste of the whole thing. It appears to me, when i formatted it, that there are places where parenthesis and scope brackets are out of place.See if this compiles:*** Source Snippet Removed ***Hope that helps,Dave

Java allows instantiation of anonymous classes. This was one example:

addWindowListener(   new WindowAdapter() {    public void windowClosing(WindowEvent e) {      System.exit(0);    }  });

There is nothing wrong with that. WindowListener is an interface. WindowAdapter is concrete implementation of interface, but with methods stubs. Using above, allows you to override a method, without defining a new class. This is commonly used in Java for event handlers. The only limitation of such declaration is, that you cannot define new methods, you can only redefine them, and you can only use classes, and cannot implement interfaces.

But I cannot help with the error, since something is missing, and the line numbers are of no help.

##### Share on other sites
haegarr    7372
See at the error's runtime stack: It complains a class cast exception anywhere in the AppletPanel class. It seems that Flora is expected to be of a type (perhaps implementing an interface in the range of Applet) what it doesn't.

But as Antheus stated above, there is little information given to us here. How are you running the Java application?

EDIT:
Quote:
 Java 1.5 DocAn applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application. The Applet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer. The Applet class provides a standard interface between applets and their environment.

##### Share on other sites
GentlemanHal    150
Hi,

It seems to me you are trying to embed your frame class in web page as an applet, or view it using the applet viewer. If you are not doing this then I don't see how the code you posted has anything to do with an applet, which it is as applet is part of the exception stack.

But if this is the case your class must extend applet and not frame.

- Chris

##### Share on other sites
Derrr, Drunken_Monkey
*Stabs himself in the hand
My teacher failed to explain to me to run it as an application and not an applet. GOD, 3 days of frustration and all I had to do was click on a different button.

##### Share on other sites
dave    2187
Ok cool, im not that profficient with Java.

##### Share on other sites
Guest Anonymous Poster
Quote:
 Original post by AntheusThe only limitation of such declaration is, that you cannot define new methods, you can only redefine them, and you can only use classes, and cannot implement interfaces.

Actually it's possible to do inner classes with interfaces, such as:

executor.execute(new Runnable() {
public void run() {
//do something
}
});

##### Share on other sites
Antheus    2409
Quote:
Original post by Anonymous Poster
Quote:
 Original post by AntheusThe only limitation of such declaration is, that you cannot define new methods, you can only redefine them, and you can only use classes, and cannot implement interfaces.

Actually it's possible to do inner classes with interfaces, such as:

executor.execute(new Runnable() {
public void run() {
//do something
}
});

True. Anything will go, as long as all methods are implemented, so abstract classes and interfaces work as well.
For some reason I assumed, that anything defined as abstract could not be used, since Adapter classes are usually provided for interfaces.

And my comment to method being inaccesible referred to this case:
Object o = new Object() {  public void hello() {}};

While this object is now defined, the only way to access the new hello() method is to use reflection.