Click JPanel does not work

1

I'm creating an interface for a system and would like it when the user clicked on a JPanel , open another window. The program is compiled normally, but by the time I click on JPanel , it does not open the desired window and exceptions are thrown:

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 resolve it.

    
asked by anonymous 12.12.2016 / 01:41

1 answer

4

As can be seen in the following excerpt 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)

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

I think you should have set up an icon for this class that should open, but the image is not localized, so it pops up nullpointer.

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

    
12.12.2016 / 03:26