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

  • SEARCH
  • Home
  • 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 6191879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:50:39+00:00 2026-05-24T02:50:39+00:00

In my app when i press Edit in my JMenuBar i receive Exception in

  • 0

In my app when i press Edit in my JMenuBar i receive

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.swing.SwingUtilities2.getLeftSideBearing(SwingUtilities2.java:241)
    at sun.swing.MenuItemLayoutHelper.getLeftExtraWidth(MenuItemLayoutHelper.java:162)
    at sun.swing.MenuItemLayoutHelper.calcExtraWidths(MenuItemLayoutHelper.java:158)
    at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:148)
    at sun.swing.MenuItemLayoutHelper.(MenuItemLayoutHelper.java:107)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferredMenuItemSize(BasicMenuItemUI.java:408)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferredSize(BasicMenuItemUI.java:363)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1634)
    at javax.swing.BoxLayout.checkRequests(BoxLayout.java:481)
    at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:298)
    at javax.swing.plaf.basic.DefaultMenuLayout.preferredLayoutSize(DefaultMenuLayout.java:60)
    at java.awt.Container.preferredSize(Container.java:1605)
    at java.awt.Container.getPreferredSize(Container.java:1590)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
    at javax.swing.JMenu.getPopupMenuOrigin(JMenu.java:383)
    at javax.swing.JMenu.setPopupMenuVisible(JMenu.java:349)
    at javax.swing.JPopupMenu.menuSelectionChanged(JPopupMenu.java:1433)
    at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117)
    at javax.swing.plaf.basic.BasicMenuUI.appendPath(BasicMenuUI.java:223)
    at javax.swing.plaf.basic.BasicMenuUI.access$200(BasicMenuUI.java:49)
    at javax.swing.plaf.basic.BasicMenuUI$Handler.mousePressed(BasicMenuUI.java:463)
    at java.awt.Component.processMouseEvent(Component.java:6265)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6033)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4629)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4294)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I cant find out what’s wrong with my app. Here is the code:

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

public class Jaba extends JFrame {
    public Jaba() {
        super("Hello Swing");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640, 480);
        setLayout(new FlowLayout());
        txt.getDocument().addUndoableEditListener(undoListener);
        add(txt);
        setJMenuBar(menuBar);
        menuBar.add(editMenu);
        editMenu.add(undoAction);
        editMenu.add(redoAction);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Jaba().setVisible(true);
            }
        });
    }

    private UndoableEditListener undoListener = new UndoableEditListener() {
        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
             undo.addEdit(e.getEdit());
        }        
    };
    private AbstractAction undoAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                undo.undo();
            } catch (CannotUndoException ex) {
                System.err.println("Cannon undo");
                ex.printStackTrace();
            }
        }
    };
    private AbstractAction redoAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                undo.redo();
            } catch (CannotUndoException ex) {
                System.err.println("Cannon undo");
                ex.printStackTrace();
            }
        }
    };
    private UndoManager undo = new UndoManager();
    private JTextArea txt = new JTextArea(40, 40);
    private JMenuBar menuBar = new JMenuBar();
    private JMenu editMenu = new JMenu("Edit");
}
  • 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-24T02:50:39+00:00Added an answer on May 24, 2026 at 2:50 am

    Use

    private AbstractAction redoAction = new AbstractAction("Redo"){...}
    

    and

    private AbstractAction redoAction = new AbstractAction("Undo"){...}
    

    Also instead of letting it explode with an exception, you can do

                if(undo.canUndo())
                {
                    undo.undo();
                }
    

    and

                if(undo.canRedo())
                {
                    undo.redo();
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to achieve something similar to Apples Contact.app. When the user press Edit
In my app after user press start button I execute some code in for
I press login button to login into my app and I'm using asyncTask for
I have an app that crashes if i press the home button, back button
When in a FB app I want to press an html button and exit
so my app listens for a long press on the search button and is
I'm writing an app where I need to simulate key press events on a
Is there anyway to close iphone app just like user press the menu key?
I want to develop an Android app in which I have key log press.
I'm like working on an app that will call home when I press down

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.