0

I'm currently using BalloonTip 1.2.4.1 in my Maven project.

I want to show / hide the BalloonTip dynamically based on the condition of user input, but failed to do so. The error is NullPointerException.

This is what I want to achieve: on application startup, the balloon will be invisible. If user enter the false input, the balloon will pop up. If user enter the right input, the balloon will disappear.

package view;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.EdgedBalloonStyle;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Frame extends JFrame {

    private JPanel contentPane;
    public JLabel lblUserID;
    public JTextField txtUserID;
    private BalloonTip bl;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame frame = new Frame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        lblUserID = new JLabel("UserID");
        lblUserID.setHorizontalAlignment(SwingConstants.CENTER);
        lblUserID.setBounds(46, 92, 69, 23);
        contentPane.add(lblUserID);
        
        txtUserID = new JTextField();
        txtUserID.setBounds(146, 92, 116, 23);
        contentPane.add(txtUserID);
        txtUserID.setColumns(10);
        txtUserID.getDocument().addDocumentListener(new DocumentListener() {    
            public void removeUpdate(DocumentEvent e) {
                check();                
            }
            
            public void insertUpdate(DocumentEvent e) {
                check();                
            }
            

            public void changedUpdate(DocumentEvent e) {
                check();                
            }
        });
        
        BalloonTipStyle edgedLook = new EdgedBalloonStyle(Color.WHITE, Color.RED);
        BalloonTip bl = new BalloonTip(txtUserID, "Please enter number as Personal ID", edgedLook, false);
        bl.setVisible(false);
        
    }
    
    private void check() {
        if (!txtUserID.getText().equals("") && txtUserID.getText().matches("\\d+")) {
            txtUserID.setBorder(new LineBorder(Color.GREEN, 1));
            if (bl.isVisible() == true) {
                bl.setVisible(false);
            }            
        } else {
            txtUserID.setBorder(new LineBorder(Color.RED, 1));
            bl.setVisible(true);
        }
    }
}
LiemLT
  • 63
  • 1
  • 3
  • 1
    1) You shouldn't name your class as `Frame` as it may be confused with `java.awt.Frame`. 2) Don't use `null-layout`, while it may seem like the easiest way to create complex UIs, it's the opposite, Swing has to deal with multiple OS, PLAFs, screen sizes and resolutions. Please take a look at [Why is it frowned upon to use null layout](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and a [very good example](https://stackoverflow.com/a/42521097/2180785) of what happens if you use it. – Frakcool Jun 29 '20 at 18:40
  • 1
    Also please take a look at [What is a NPE](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), *"The error is NullPointerException."* well, a Stack Trace might be of more help than just that phrase. And you should avoid this kind of comparissons: `if (bl.isVisible() == true)`, `isVisible` already returns a `boolean`, you might end up with an assignation instead of comparison if you don't. So `if (bl.isVisible())` is a much cleaner and safer solution. – Frakcool Jun 29 '20 at 18:44

0 Answers0