Hi,
This GUI will compile but does not run. I know it is something to do with panel 'p2' as when I remove it, it runs fine.
I have posted the error below. Help would be greatly appreciated.
Thanks.
Error: Exception in thread "main" java.lang.NullPointerException
at TestSwingCommonFeatures.<init>(TestSwingCommonFeat ures.java:28)
at TestSwingCommonFeatures.main(TestSwingCommonFeatur es.java:41)
import javax.swing.*; import java.awt.*; import javax.swing.border.*; public class TestSwingCommonFeatures extends JFrame { private JPanel p1, p2; private JButton jbtLeft, jbtCenter, jbtRight; private JLabel jlblRed, jlblOrange; public TestSwingCommonFeatures() { p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2)); p1.add(jbtLeft = new JButton("Left")); p1.add(jbtCenter = new JButton("Center")); p1.add(jbtRight = new JButton("Right")); p1.setBorder(new TitledBorder("Three Buttons")); jbtLeft.setBackground(Color.WHITE); jbtCenter.setForeground(Color.GREEN); jbtRight.setToolTipText("This is the right button"); setLayout(new GridLayout(2,1,5,5)); p2.add(jlblRed = new JLabel("Red")); p2.add(jlblOrange = new JLabel("Orange")); p2.setBorder(new TitledBorder("Two Labels")); Font largeFont = new Font("TimesRoman", Font.BOLD, 20); LineBorder lineBorder = new LineBorder(Color.BLACK, 2); setLayout(new GridLayout(2,1,5,5)); add(p1); add(p2); } public static void main(String[] args) { TestSwingCommonFeatures frame = new TestSwingCommonFeatures(); frame.setTitle("TestSwingCommonFeatures"); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }