0

I'm working on a chat client/server. In the client, I have a class called ClientGui, and a subclass called InputPane which extends JPanel. ClientGui handles the major parts of the GUI related client things, but InputPane only handles the input from the user.

I keep getting a strange NullPointerException when trying to set the font in my InputPane object in the ClientGui class. Here is some of my code:

public ClientGui() {
    // configure JFrame

    this.serverOutput = new JTextPane();
    this.input = new InputPane();

    // configure JScrollPane

    // configure other objects

    // various set up methods

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.add(input, BorderLayout.PAGE_END);
}

As you can see, the input object is being initialized. Here is its constructor:

public InputPane() {
        this.inputField = new JTextField();
        this.send = new JButton("Send");

        // set size

        this.setFont(getFont());

        // various methods

        // add components
    }

When I try to set the font, it throws a NullPointerException. Through various println() calls, I was able to find out that inputField and send are null. I tried putting println() methods in the constructor, but none were called (and/or printed).

I am very confused by this. If anyone could give an explanation, it would help a lot. Thank you.

EDIT: Here is the StackTrace:

Exception in thread "main" java.lang.NullPointerException
at net.dean.tcp.client.gui.ClientGui$InputPane.setFont(ClientGui.java:518)
at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source)
at javax.swing.plaf.basic.BasicPanelUI.installDefaults(Unknown Source)
at javax.swing.plaf.basic.BasicPanelUI.installUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JPanel.setUI(Unknown Source)
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at net.dean.tcp.client.gui.ClientGui$InputPane.<init>(ClientGui.java:496)
at net.dean.tcp.client.gui.ClientGui.<init>(ClientGui.java:138)
at net.dean.tcp.client.gui.ClientGui.main(ClientGui.java:44)

EDIT 2: Here is the ClientGui$InputPane.setFont(Font) method:

public void setFont(Font f) {
        super.setFont(f);
        inputField.setFont(f);
        send.setFont(f);

        Dimension size = getSizeFromFont(); 
        // Dimension based on getFont()'s height

        send.setPreferredSize(size);
        inputField.setPreferredSize(size);
    }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
mattbdean
  • 2,532
  • 5
  • 26
  • 58

2 Answers2

4

The problem is that the Swing framework calls setFont as part of the JPanel constructor. Since this executes before the body of the constructor, InputPane.setFont is being called before the send and inputField member variables are initialized. The only clean solution here, I think, is to do an explicit null check inside InputPane.setFont:

public void setFont(Font f) {
    super.setFont(f);
    if (inputField != null) {
        inputField.setFont(f);
    }
    if (send != null) {
        send.setFont(f);
    }

    Dimension size = getSizeFromFont(); 
    // Dimension based on getFont()'s height

    if (send != null) {
        send.setPreferredSize(size);
    }
    if (inputField != null) {
        inputField.setPreferredSize(size);
    }
}

(You can probably reorganize your calls to reduce the number of null checks.)

As an aside, this is a good example of why it is bad practice to call an overridable method from a constructor. But Swing is what it is, and there's no profit in whining about it, so I'll stop here. :)

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

When you are setting the font using setFont and you are getting it from getFont method and setting it. It is more likely that this getFont() returns NULL and that may be the reason. Also in my opinion neither inputField nor the send object can be NULL in inputPane object unless you make them NULL explicitly.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • 1
    As I said in the question comments, "The line it throws the NullPointerException is where it is setting inputField's font. The font variable is not null, but inputField is." – mattbdean Oct 18 '12 at 04:44
  • @whowantsakookie not sure from exceptions details, for better help sooner post an SSCCE, because you mixing properties with fields that aren't initialized or they are ???, – mKorbel Oct 18 '12 at 12:41
  • @mKorbel I was trying to go for an SSCCE. – mattbdean Oct 18 '12 at 20:21