className.main(new String[]{"filename.txt"}); - Help
Can anybody explain to me what that line of code does? I've seen it before in the main method but I don't understand, why is it different to when we just create an instance of a class?
I'm used to seeing things like:
public static void main(String[] args) {
Class object = new Class();
object.method();
}
One more question, if instead of placing "className.main(new String[]{"filename.txt"});" in the main method I want to use actionlistener to call that class when a button is clicked, what do I have to put in the actionlistener body? I hope my question is not too confusing.
Re: className.main(new String[]{"filename.txt"}); - Help
Quote:
className.main(new String[]{"filename.txt"});
That statement calls the static method main in the class named className and passes it a String array containing one element: filename.txt
Quote:
I want to use actionlistener to call that class when a button is clicked, what do I have to put in the actionlistener body
What do you mean by "call that class"? The above statement will start the class executing via its main method. The other way to create a class is to use the new statement. What happens next depends on what the class's constructor does.
Re: className.main(new String[]{"filename.txt"}); - Help
Hi Norm (again)
Yesterday I posted a similar question but I think it was on "what's wrong with my question" page, you couldn't help me much as I didn't give you too much information on what the program does.
What I mean by "call that class" is just "start it" (I'm still learning the programming terminology, sorry about that). I thought of copying and pasting my code so you could help me yesterday but it's horribly long, it has like 8 java class files. However, the problem I have is just when I try to call or "to start" one particular class which looks like this in the main method:
Code :
public static void main(String args[]) {
PApplet.main( new String[]{"projectII.Cube"} );
}
So, I don't want the class to start automatically but I want to use a button so that when it's clicked the class runs, so what I did was I deleted that line from the main method and I created an instance of the class in the actionlistener body:
Code :
Cube myCube = new Cube();
myCube.setup();
myCube.draw();
As soon as I click the button in my GUI I get an error:
Code :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at processing.core.PApplet.size(Unknown Source)
at processing.core.PApplet.size(Unknown Source)
at projectII.Graphs.setup(Graphs.java:25)
at projectII.ActionListenerWin$Action1.actionPerformed(ActionListenerWin.java:94)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I could copy and past my entire code but like I said, all classes with actionlistener run fine except for those that have Processing libraries, for example:
Say I want to start this class when I click the button
Code :
package projectII;
import processing.core.*;
import processing.serial.*;
public class Cube extends PApplet{
float a;
void setup() {
size(450, 450, OPENGL);
fill(238,203,173, 100);
stroke(248,248,255);
// fill(0,0,205, 100); Blue colour
}
void draw() {
background(0);
translate(width/2, height/2);
rotateX(a);
rotateY(a*2);
sphere(100);
//sphereDetail(20,20);
rotateX(PI/2);
//rotateZ();
a += 0.01;
}
Then in actionlistener I write:
Code :
button.addActionListener(new Action1());
static class Action1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
Cube myCube = new Cube();
myCube.setup();
myCube.draw();
}
}
And then I get the error, what am I doing wrong?
I hope this information helps, let me know if you anything else, thanks
Re: className.main(new String[]{"filename.txt"}); - Help
Quote:
java.lang.NullPointerException
at processing.core.PApplet.size(Unknown Source)
at processing.core.PApplet.size(Unknown Source)
at projectII.Graphs.setup(Graphs.java:25)
at projectII.ActionListenerWin$Action1.actionPerforme d(ActionListenerWin.java:94)
There is a variable with a null value at line 25 in the Graphs class. Look at that line and see what variable is null then find out why it does not have a valid value.
Re: className.main(new String[]{"filename.txt"}); - Help
I've seen that line before and what it says is:
setup(500,500); // it is size of the screen, that normally works except when I use the action listener.
Thanks anyway.