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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:07:15+00:00 2026-05-27T20:07:15+00:00

I am using mouse listener to know when a user clicks on nodes of

  • 0

I am using mouse listener to know when a user clicks on nodes of a JTree. Although when the user clicks on the arrow for expansion of a node(view childs) the following exception is thrown:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Core.ChannelView$1.mousePressed(ChannelView.java:120)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
    at java.awt.Component.processMouseEvent(Component.java:6370)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

ChannelView listener :

MouseListener ml = new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
                if (e.getClickCount() == 1) {
line 120>>>>>        System.out.println(selPath.getLastPathComponent());

                } else if (e.getClickCount() == 2) {
                    System.out.println("Double" +selPath.getLastPathComponent());
                }
            }
        };
        tree.addMouseListener(ml);

Any suggestions about how should I handle this case ? Should I simply try-catch inside the if statement? Also is this a good way to check for double-clicks or I should be doing it with a different method ? Thanks

  • 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-27T20:07:16+00:00Added an answer on May 27, 2026 at 8:07 pm

    Your listener tries to get the node at the mouse location. If there isn’t any node, null is returned by tree.getPathForLocation(). Just test if selPath is null before calling a method on it:

    if (selPath == null) {
        System.out.println("No node at this location");
    }
    else {
        if (e.getClickCount() == 1) {
        ...
    }
    

    And yes, getClickCount() returns the number of clicks associated with the event, so it seems appropriate to check if it’s a double or a simple click.

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

Sidebar

Related Questions

In my Direct3D application, the camera can be moved using the mouse or arrow
Writing a code that does the following: Adds a mouse listener to a web-page
When I scroll in Emacs using mouse wheel, it scrolls 5 lines at a
Basically what I'm trying to do is have a picture rotate using mouse events.
I often (using the mouse) switch between 1 of 3 favorite folders under my
We'd like to override DataGridView's default behavior when using a mouse wheel with this
Using the Google Maps API v3: How do I change the mouse cursor when
I am using bitmapData and bitmap classes to render a mouse cursor on the
how can I set the mouse cursor position in an X window using a
Is there any way to determine if the mouse scrolls up or down using

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.