开发者

NullPointerException with JFrame

开发者 https://www.devze.com 2023-04-06 20:51 出处:网络
At the moment all I want this program to do is run without any compile errors. Basically, what I need it to do is open the frame and then when the frame is selected, if I press the up arrow key it wil

At the moment all I want this program to do is run without any compile errors. Basically, what I need it to do is open the frame and then when the frame is selected, if I press the up arrow key it will set arrows[0] to true and when I release it it will set it to false (same with right, down, and left as well)...

My code will compile. However, I keep getting this error when I try to run it.

java.lang.NullPointerException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

I've done a program somewhat similar to this before and I never had this problem. I originally thought it was because of the "frame.addKeyListener;" or the "frame.setFocasable(true);" but I tried taking those lines out and it still came up with the error...

Here's the code that I am running, any help to fix this problem would be helpful.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;

public class arrowTest extends JApplet implements KeyListener {

  private boolean[] arrows = new boolean[4];
  private int x = 0;
  private int y = 0;

  public arrowTest() {

  }

  // Handle the key typed event from the text field.
  public void keyTyped(KeyEvent e) {
    System.out.println("KEY TYPED: ");
  }

  // Handle the key-pressed event from the text field.
  public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_UP) {
      arrows[0] = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      arrows[1] = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
      arrows[2] = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      arrows[3] = true;
    }

  }

  public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_UP) {
      arrows[0] = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      arrows[1] = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
      arrows[2] = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
      arrows[3] = false;
    }

  }

  public void run() {

    JFrame frame = new JFrame();
    JApplet applet = new arrowTest();
    frame.add(applet);
    frame.setTitle("arrowTest");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(40开发者_开发问答0, 200);
    frame.setVisible(true);
    frame.addKeyListener(this);
    frame.setFocusable(true);

  }

  public void main(String[] args) {

    run();

  }
}


At the moment all I want this program to do is run without any compile errors.

For the record, your program does run without "compile errors". What's happening is you're getting an null pointer exception (NPE) thrown during the running of the program, not a compile error. You need to find out what line is causing your NPE and then fix it by making sure all reference variable have been initialized before using them.

But also, you should not be using KeyListeners for this but instead use Key Bindings -- there's a big difference. The Key Binding tutorial will explain all. It may appear a bit daunting at first, but don't give up, follow the examples, and you'll be using it in no time.

And why are you using JApplet anything when you're trying to create a JFrame? That's just a bit funky.

Edit 2
And your code won't even run since your main method is non-static. If you're running your program as an actual JFrame, then you'll need to show us the actual code that you've used. This code has no viable main method and thus isn't it.

Edit 3
No, static isn't your problem, still you need to learn to use key binding and to not mix JApplet and JFrame code in this strange chimera you've got.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号