Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: className.main(new String[]{"filename.txt"}); - Help

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: className.main(new String[]{"filename.txt"}); - Help

    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
    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.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

           Cube myCube = new Cube();
               myCube.setup();
               myCube.draw();

    As soon as I click the button in my GUI I get an error:

     
    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

    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:

     
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: className.main(new String[]{"filename.txt"}); - Help

    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.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  2. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  3. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM