Click Jpanel does not work

Asked

Viewed 104 times

1

I am creating an interface for a system and would like that when the user clicks on a JPanel, open another window. The program is compiled normally, but at the moment I click on JPanel, does not open the desired window and exceptions are launched:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at telas.Venda.initComponents(Venda.java:66)
at telas.Venda.<init>(Venda.java:22)
at telas.TelaProprietario.vendaMouseClicked(TelaProprietario.java:935)
at telas.TelaProprietario.access$2400(TelaProprietario.java:15)
at telas.TelaProprietario$13.mouseClicked(TelaProprietario.java:616)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I created the interface through Netbeans. Below is part of the code:

package telas;

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

public class TelaProprietario extends javax.swing.JFrame
{
public TelaProprietario()
{
    initComponents();
}

private void initComponents()
{
    tela=new javax.swing.JPanel();
    venda=new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setMinimumSize(new java.awt.Dimension(1024,768));

    tela.setBackground(new java.awt.Color(204,204,255));
    tela.setMinimumSize(new java.awt.Dimension(1024,768));
    tela.setPreferredSize(new java.awt.Dimension(1024,768));

    venda.setBackground(new java.awt.Color(204,204,255));
    venda.setPreferredSize(new java.awt.Dimension(150,150));
    venda.addMouseListener(new java.awt.event.MouseAdapter()
    {
        public void mouseClicked(java.awt.event.MouseEvent evt)
        {
            vendaMouseClicked(evt);
        }
        public void mouseEntered(java.awt.event.MouseEvent evt)
        {
            vendaMouseEntered(evt);
        }
        public void mouseExited(java.awt.event.MouseEvent evt)
        {
            vendaMouseExited(evt);
        }
    });

    // código de layout
}

private void vendaMouseEntered(java.awt.event.MouseEvent evt)
{
    venda.setBackground(new Color(255, 255, 255));
}

private void vendaMouseExited(java.awt.event.MouseEvent evt)
{
    venda.setBackground(new Color(204, 204, 255));
}

private void vendaMouseClicked(java.awt.event.MouseEvent evt)
{
    new Venda().setVisible(true); // outra tela
    setVisible(false);
}

public static void main(String args[])
{
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            new TelaProprietario().setVisible(true);
        }
    });
}

private javax.swing.JPanel tela;
private javax.swing.JPanel venda;
}

I would like to know what is causing this problem and how to solve it.

1 answer

4


As can be seen in the next section of your error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at telas.Venda.initComponents(Venda.java:66)
at telas.Venda.<init>(Venda.java:22)
at telas.TelaProprietario.vendaMouseClicked(TelaProprietario.java:935)

Error occurs when trying to open class Venda, which apparently is another screen, after mouse click action in the class TelaProprietario.

I believe you must have set some icon for this class that you should open, but the image is not located, so pop nullpointer.

Check line 66 of your Sale class, which is within the method initComponents, because the image path you are setting to represent the icon of this window class is invalid.

  • 1

    That was the problem. An icon of the Venda class was on the wrong track. It was my lack of attention. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.