Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:56:20+00:00 2026-05-25T20:56:20+00:00

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

  • 0

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(400, 200);
    frame.setVisible(true);
    frame.addKeyListener(this);
    frame.setFocusable(true);

  }

  public void main(String[] args) {

    run();

  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:56:21+00:00Added an answer on May 25, 2026 at 8:56 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Oracle 10g at the moment. I want to search through all
I'm trying to do something very simple. All I want to do at this
At the moment, our application uses the Trident Win32 component, but we want to
At the moment my code looks like this: # Assign values for saving to
I am looking for an inverse version of RunOnceEx. RunOnceEx does run some program,
I'm using LINQ to query my database, and I don't want all the records
Specifically I want all PasswordBox's using a Style to have the same static KeyDown
At the moment my code (PHP) has too many SQL queries in it. eg...
At the moment I am working on a project admin application in C# 3.5
At the moment we use HSQLDB as an embedded database, but we search for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.