• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with taking the arguments

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class JedApplet extends Applet {

public void init() {
// her comes code that create SimpleUniverse with Information=0, and
// whwn I clik with mouse on Button field, I have to put new
// SimpleUniverse with Information=1
}
......
}

class Controls extends Panel implements ActionListener {
Canvas3D c;
int Information;

public Controls(Canvas3D c) {
Button b = null;
this.c = c;
b = new Button("Button");
b.addActionListener(this);
add(b);
}

public void actionPerformed(ActionEvent ev) {
String label = ev.getActionCommand();
Information =1;
//??? Need to send Information from here to public void init()
}
}

I need to give Information from this ??? to public void init() so I can get different picture on my monitor when I clik the mouse. I know it is basic question but I really need help!!!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You should not construct any part of the GUI in the init method. That method is part of the applet lifecycle, and is called just once when the applet is started, and never again after that.

So you should define your own method that takes any parameters it needs to, and use that instead of the init method.
 
Luka Juric
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but still I can't do what I want.
I try like this, but have

public class JedApplet extends Applet {
JedApplet jedapplet

public void init() {
// her comes code that create SimpleUniverse with Information=0, and
// whwn I clik with mouse on Button field, I have to put new
// SimpleUniverse with Information=1
paint(Information);
}

public void paint(int Information){
//adding something in the scene
}

public void redraw(int Information){
paint(Information);
}

public static void main(String[] args) {
Frame frame = new MainFrame(new JedApplet(), 500, 500);
}
}

class Controls extends Panel implements ActionListener {
Canvas3D c;
int Information;
JedApplet jedapplet;

public Controls(Canvas3D c) {
Button b = null;
this.c = c;
b = new Button("Button");
b.addActionListener(this);
add(b);
}

public void actionPerformed(ActionEvent ev) {
String label = ev.getActionCommand();
Information =1;
jedapplet.redraw(Information);
}
}


Most probably something is wrong with this bold one and giving me
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in this last bold line of the code.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never assign anything to field "jedapplet", so it's value remains null, and you will get a NullPointerException if you try to invoke one of its methods.

I'm guessing that you should assign the field jedapplet in class JedApplet with the anonymous object you're creating in the main method. Then you should pass that object as parameter to the constructor of class Controls -wherever that is called-, and assign it to field jedapplet of class Controls.

Please use the "CODE" button to surround code of any length with code tags to preserve its formatting.
[ March 07, 2007: Message edited by: Ulf Dittmer ]
 
Luka Juric
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


First thanks for trying to explain me
I know this is the basic, but it is very hard to learn it by yourself.
If I fill jedapplet like bold on the top I get Exception in thread "main" java.lang.StackOverflowError, and if I fill it like italic I get Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
[ March 07, 2007: Message edited by: Luka Juric ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

It seems that you understand why you got the NullPinterException.

You got the stack overflow because every time a new instance of JedApplet is created, it in turn creates another new instance of JedApplet, until all memory is filled up with JedApplet instances.
 
Luka Juric
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it works!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic