0

I am creating a 'Troll' like application to use in a YouTube video, and I want a JFrame to jump around the screen every 1/2 second.

Heres my code:

package troll1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.Timer;

public class troll extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;

    public static void main(String args[]) {
        troll w1 = new troll();
        w1.setVisible(true);
        w1.setSize(500,500);
    }

    public Random ran;
    public int random;
    public troll w1;
    public Timer t;

    public troll() {
        Timer t = new Timer(500, this);
        t.start();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Random ran = new Random();

        for(int counter=1; counter<=1;counter++) {
            int random = 1+ran.nextInt(500);
            w1.setLocation(random, random);
        }

    }

}

The problem is, when I run this code, I get a error saying,

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at troll1.troll.actionPerformed(troll.java:37)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

Also, once I get this error gone, I will make it so the JFrame jumps around the entire screen, not just 500,500 and below.

hotzst
  • 7,238
  • 9
  • 41
  • 64
Jbot
  • 1
  • 2
  • `w1` in your action listener is never instantiated. You seem to be confused about variable scopes. You have class members with the same name as local variables (the w1 in your main() is not the same reference as the w1 in your class. same with the `ran` reference in your action listener, and the `t` reference in your constructor). – Cypher Mar 25 '16 at 16:34
  • How, I looked at that and I still can't find out. – Jbot Mar 25 '16 at 16:37
  • 1
    http://www.java-made-easy.com/variable-scope.html – Cypher Mar 25 '16 at 16:37
  • Class names SHOULD start with an upper case character. "troll" is NOT a proper class name. – camickr Mar 25 '16 at 16:55

1 Answers1

0

the w1 variable is always null is never valorized. you can use this in actionPerformed to make it work this way

 @Override
public void actionPerformed(ActionEvent arg0) {
    Random ran = new Random();

    for(int counter=1; counter<=1;counter++) {
        int random = 1+ran.nextInt(500);
        this.setLocation(random, random);
    }

}
Romeo Sheshi
  • 901
  • 5
  • 7