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 8891657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:48:03+00:00 2026-06-14T22:48:03+00:00

I am writing a graphical user interface program in Java that implements ActionListener. The

  • 0

I am writing a graphical user interface program in Java that implements ActionListener. The program is a guitar chord visualizer using 2D graphics. I have a toolbar where the user selects what chord to be displayed. So in the actionPerformed(ActionEvent e) method, when the user selects a certain chord, that choice calls a method where the chord is displayed. However, when I test the program I get tons of errors. Here are the error messages

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at fretboard.displayAMajor(fretboard.java:493)
    at fretboard.actionPerformed(fretboard.java:74)
    at java.awt.MenuItem.processActionEvent(MenuItem.java:650)
    at java.awt.MenuItem.processEvent(MenuItem.java:609)
    at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:343)
    at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:331)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:663)
    at java.awt.EventQueue$2.run(EventQueue.java:661)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:677)
    at java.awt.EventQueue$3.run(EventQueue.java:675)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Also, I am not using JPanel or JFrame. I am simply using Frame.
What is wrong with my program? Thanks!

Here is some of my code (The program is over 500 lines as of now).

public class fretboard extends Frame implements ActionListener{
    public static void main(String[] args) {
        Frame frame = new fretboard();
        frame.setSize(1280, 960);
        frame.setVisible(true);
    }


    /**
     * Create the menu bar and set title
     */

    public fretboard() {
        // Change the title of the window
        setTitle("Fretboard");
        // Create a menu bar where user will be given choice of chords
        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        Menu menu = new Menu("Chords");
        mb.add(menu);
     }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if("A Major".equals(command)) {
            displayAMajor();
        }
This method contains the bulk of my program. Here are just a few lines.

    public void paint(Graphics g) {
        // Declare local variables
        int h = 40, w = 26, x = 695, y = 230;
        Graphics2D g2 = (Graphics2D) g;
        Font font = new Font("SansSerif", Font.BOLD, 28);
        Font font1 = new Font("SansSerif", Font.BOLD, 18);          
        // Declare the note variables
        // First string
        Ellipse2D E1 = new Ellipse2D.Double(x, y-110, w, h);
        // Open the image
        File fretBoardFile = new File("/Users/macbook/desktop/Gibson_Fretboard.jpg");
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(fretBoardFile);
            g.drawImage(bi, 25, 25, null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Draw notes
        // Draw the E note on the open 1st string
        // Change color to blue
        g2.setColor(Color.blue);
        g2.draw(E1);
        g2.fill(E1);
        g2.setColor(Color.white);
        g2.setFont(font);
        g2.drawString("E", x+5, y-80);
        // Change color back to blue
        g2.setColor(Color.blue);

public void displayAMajor() {
    // Declare local variables
    int h = 40, w = 26, x = 695, y = 230;
    Graphics g = null;
    Graphics2D g2 = (Graphics2D) g;
    //Graphics2D g2 = new Graphics();
    Font font = new Font("SansSerif", Font.BOLD, 28);
    // Declare notes
    Ellipse2D E1 = new Ellipse2D.Double(x, y-110, w, h);
    // Display notes for the A Major chord
    // Draw the E note on the open 1st string
    // Change color to red
    g2.setColor(Color.red);
    g2.draw(E1);
    g2.fill(E1);
    g2.setColor(Color.white);
    g2.setFont(font);
    g2.drawString("E", x+5, y-80);
    // Change color back to blue
    g2.setColor(Color.blue);
    repaint();
}
  • 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-06-14T22:48:04+00:00Added an answer on June 14, 2026 at 10:48 pm

    Aha, casting variables, null, graphics, I’m going to take a guess — you’re trying to save the Graphics object as a class field, you’re casting it to Graphics2D, and it’s null. If so, you’ll want to read up on how to do drawing with Swing because that’s not how it’s done. You must use the Graphics object provided by the JVM and passed into a JComponent’s (such as a JPanel’s) paintComponent(Graphics g) method. This is a decent link to start learning about Performing Custom Painting in Swing.

    And again, you’ll want to refactor your code because your class sounds way too big. Also you’ll want to learn and use Java naming conventions. Class names should start with upper case letters for instance.

    Yikes, it’s even worse:

    Graphics g = null;  // *******  this is null!!!!
    Graphics2D g2 = (Graphics2D) g;  // ***** it's *STILL* null!!
    //Graphics2D g2 = new Graphics();
    Font font = new Font("SansSerif", Font.BOLD, 28);
    Ellipse2D E1 = new Ellipse2D.Double(x, y-110, w, h);
    g2.setColor(Color.red);   // ***** it's *STILL* null!!
    g2.draw(E1);   // ***** it's *STILL* null!!
    g2.fill(E1); // **** etc...
    

    You’re using a variable that is null, so it should be no surprise that it throws a NPE. Please read the tutorial that I’ve linked to.

    You’ll want to do your drawing either in the paintComponent(...) method override of a JPanel or other JComponent-derived class, either that or in a BufferedImage by extracting its Graphics object and drawing with that. If you’re creating static images such as chord tabs, then BufferedImages will likely be the way to go, and then display them either in a ImageIcon held in a JLabel or in a paintComponent method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing a graphical interface, using Java, what's the appropriate way of switching between
I'm currently writing a graphical sudoku solver program in Java using Swing/Awt. So I
I'm writing a graphical user interface to plot data on a xy-axis. It's written
I'm writing a plug-in for a program where I have a custom user control
I'm writing a little app that displays some graphics and just now I'm trying
I'm writing an interface that features a large (~50000px width) canvas-type area that is
I'm writing a painting/graphics Java application for a mobile phone (so memory is limited).
So basically I'm writing a program where the user clicks and drags the mouse
I'm writing a program that draws a rotating cube (with texture) in the middle
hi there i was writing a module for opencart that let's the user chose

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.