fork(1) download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. public final class Main {
  5.  
  6. private final Map<Character, Runnable> map;
  7.  
  8. public Main() {
  9. map = new HashMap<>();
  10. map.put('h', () -> moveCursorLeft());
  11. map.put('j', () -> moveCursorDown());
  12. map.put('k', () -> moveCursorUp());
  13. map.put('l', () -> moveCursorRight());
  14. }
  15.  
  16. public void handleKeyInput() {
  17. var c = getPressedKey();
  18. var action = map.get(c);
  19. // Since 'action' can be null, the next statement may throw
  20. // a NullPointerException.
  21. action.run();
  22. }
  23.  
  24. /**
  25.   Returns the character of the pressed key.
  26.   If no key is pressed, blocks until any key is pressed.
  27.  
  28.   @return
  29.   The character of the pressed key.
  30.   */
  31. private char getPressedKey() {
  32. return 'a';
  33. }
  34.  
  35. private void moveCursorLeft() {}
  36. private void moveCursorDown() {}
  37. private void moveCursorUp() {}
  38. private void moveCursorRight() {}
  39.  
  40. public static void main(String[] args) {
  41. var m = new Main();
  42. m.handleKeyInput();
  43. }
  44. }
  45.  
Runtime error #stdin #stdout #stderr 0.06s 33608KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at Main.handleKeyInput(Main.java:21)
	at Main.main(Main.java:42)