• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help: Exception occurred during event dispatching

 
daphonk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there.
Here is my code, "I get Exception occurred during event dispatching"
who can help me ?
import java.awt.*;
import java.awt.event.*;
public class Forier extends Frame

{
int i=0;
int Anzahl=0;
int Ende=0;
int p=1;
CMeineCanvas Malen;
CMeineCanvas2 Malen2;
Panel P1;
Panel P2;
Forier Fenster;

public static void main(String[ ] args)
{
Forier Fenster = new Forier();

Fenster.pack();
Fenster.setBackground(Color.lightGray);
Fenster.setForeground(Color.red);
Fenster.setSize(700,500);
Fenster.show();

}

public Forier()
{
super("titel");
setLayout(new GridLayout(1,2,20,20));
Panel P1 =new Panel();
Panel P2 =new Panel();
Panel P3 =new Panel();
Panel P4 =new Panel();

P1.setLayout(new FlowLayout());
Button f1 = new Button("Neue Oberwelle");
P1.add(f1);
P2.setLayout(new FlowLayout());
CMeineCanvas Malen = new CMeineCanvas();
P2.add(Malen);
CMeineCanvas2 Malen2 = new CMeineCanvas2();
P2.add(Malen2);
addWindowListener(new WindowLauscher());
f1.addActionListener(new ActionLauscher());
add(P1);add(P2);//add(P3);add(P4);
}




class WindowLauscher extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

class ActionLauscher implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String Label;
Label=e.getActionCommand();
if(Label.equals("Neue Oberwelle"))
Ende = Ende+2;

Malen.repaint();
Malen2.repaint();




}
}
class CMeineCanvas extends Canvas
{
CMeineCanvas(){

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

double x,y,z;
int xpos,ypos;

g.translate(0,0);
g.translate(150,150);
g.setColor(Color.yellow);
g.drawLine(0,-100,0,100);
g.drawLine(-150,0,150,0);
g.drawString("-3",-147,12);
g.drawString("-3",4,98);
g.drawString("+3",135,12);
g.drawString("+3",4,-98);
g.setColor(new Color(255,255,0));

for(Anzahl=1; Anzahl<=Ende; Anzahl=Anzahl+2)
{
for(x=-3.0; x<=3;x=x+0.005)
{
y=(Math.sin(Anzahl*x)/Anzahl);

xpos=(int)(x*50);
ypos=(int)(-y*50);
g.fillOval(xpos,ypos,1,1);
}
}
}
public Dimension getMinimumSize(){
return new Dimension(300,300);
}
public Dimension getPreferredSize(){
return getMinimumSize();
}
}
class CMeineCanvas2 extends Canvas
{
CMeineCanvas2(){

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

double x,y,z;
int xpos,ypos;

g.translate(150,150);
g.setColor(Color.yellow);
g.drawLine(0,-100,0,100);
g.drawLine(-150,0,150,0);
g.drawString("-3",-147,12);
g.drawString("-3",4,98);
g.drawString("+3",135,12);
g.drawString("+3",4,-98);
g.setColor(new Color(255,255,0));

for(x=-3.0; x<=3;x=x+0.005)
{
y=0;
double sum=0;

for(p=1; p<=Ende; p=p+2)
{
y=(Math.sin(p*x)/p);
sum = sum + y;
}

xpos=(int)(x*50);
ypos=(int)(-sum*50);
g.fillOval(xpos,ypos,1,1);

}
}
public Dimension getMinimumSize(){
return new Dimension(300,300);
}
public Dimension getPreferredSize(){
return getMinimumSize();
}
}
}




 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your error is actually NullPointerException!
You are not assigning any value to your class variables Malen and Malen2. They default to a value of null and you don't reassign them to anything.
Ah, you say, but I do! Look inside my constructor!
Well, I say, lets discuss the lines in question:
CMeineCanvas Malen = new CMeineCanvas();
CMeineCanvas2 Malen2 = new CMeineCanvas2();
The two lines above will create 2 local variables named: Malen and Malen2 and assign them some value. Unfortunately this doesn't change the class variables also named Malen and Malen2. In order to change those we don't create any local variables but instead just assign them and use them. It would look like:
Malen = new CMeineCanvas();
Malen2 = new CMeineCanvas2();
Then the class variables will have non-null values and things should work.
By the way, your name doesn't adhere to the naming policy here at JavaRanch. Please read the policy here and re-register with an appropriate name.
Regards,
Manfred.

[This message has been edited by Manfred Leonhardt (edited December 06, 2001).]
 
daphonk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot ...
(I will change my name immediatelly)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!