-1

I am creating a KeyEvent for my Main class, what I want the event to do is when the UP key is pressed, it will return n as false, and when the DOWN key is pressed, it will return n as true.

package main;

import java.awt.event.KeyEvent;

public class IsStopped {

    public static boolean main(String[] args, KeyEvent e) {
        boolean n = false;

        int key = e.getKeyCode();
        if (key == KeyEvent.VK_UP) {
            return n = false;
        }

        if (key == KeyEvent.VK_DOWN) {
            return n = true;

        }
        return n;
    }
}

I want to use n for my Main class. Like this inside a loop:

if(main.IsStopped.main(args, null) == false) {
break;

This is what returns once I run my Main class:

Exception in thread "main" java.lang.NullPointerException at main.IsStopped.main(IsStopped.java:12) at main.Main.main(Main.java:27)

How would I define e?

YCF_L
  • 49,027
  • 13
  • 75
  • 115
conku
  • 79
  • 6
  • 1
    The `null` you are passing to main, that is `e`. – NickL Jan 12 '17 at 21:39
  • Check [this](http://stackoverflow.com/a/30011731/4834682) out. – Null Jan 12 '17 at 21:39
  • Your code doesn't make the least bit of sense. If you're going to pass null to yourself you have to defined the called code against NPEs, obviously, but the real problem is that you only have access to `KeyEvents` from with a key-event handler, and you aren't within one. You can't write this code as it is currently designed. NB your class and methods are excruciatingly poorly named. – user207421 Jan 12 '17 at 22:38

1 Answers1

1

If you try to use this piece of code, then will not work with you because:

the main method should look like this ;

public static void main(String[] args) {
   //your code
}

and not like this :

public static boolean main(String[] args, KeyEvent e) {
   //your code
}

So I suggest to create a separate method like this:

//main class
public static void main(String[] args) {
    KeyEvent key = ///;
    //call your test methode with your KeyEvent
    myTest(key);

   //For example:
   Button a = new Button("click");
   KeyEvent key = new KeyEvent(a, 1, 20, 1, 10, 'a');
   System.out.println("---->" + myTest(key));

}

 //make your test here
public static boolean myTest(KeyEvent e) {
    boolean n = false;

    int key = e.getKeyCode();
    if (key == KeyEvent.VK_UP) {
        return n = false;
    }

    if (key == KeyEvent.VK_DOWN) {
        return n = true;

    }
    return n;
}

You can learn more here:

Getting started (Oracle tutorial)

The Java Main Method

Anatoly Shamov
  • 2,364
  • 1
  • 13
  • 22
YCF_L
  • 49,027
  • 13
  • 75
  • 115
  • I "suspect" that the OP has just created their own method which they intend to call themselves, your solution wouldn't actually solve their problem, but does highlight part of the cause - at least from perspective :P – MadProgrammer Jan 12 '17 at 21:51
  • yes, i already edit my answer @MadProgrammer – YCF_L Jan 12 '17 at 21:53