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);
}