Hello guys,
Basically im trying to do a Insurance Application form in java. And it uses multiple JPanels in a JFrame.
-adding of JPanel into main program frame was done like this:
//jpCenterArea to hold jp1-jp7
jpCenterArea.add(jp1);
jpCenterArea.add(jp2);
jpCenterArea.add(jp3);
jpCenterArea.add(jp4); ...etc
********Add Jpanels to JFrame*****/
add(jpTitle, BorderLayout.NORTH);
add(jpCenterArea, BorderLayout.CENTER);
add(jpBottom, BorderLayout.SOUTH);
However, even though program can compile,
it cannot be run. error as mentioned below:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl<Container.java:1045>
at java.awt.Container.add<Container.java:365>
at TravelInsuranceApplication.<init>TravelInsuranceApplication.java:120>
at TravelInsuranceApplication.main<TravelInsuranceApplication.java:154>
1 import javax.swing.*;
2 import java.awt.*;
3 public class TravelInsuranceApplication extends JFrame
4 {
5 //declare private variables
6 private JLabel jlblTitle, jlblName, jlblNRIC, jlblAdd, jlblPostal, jlblContact, jlblDOB, 7 jlblEmail, jlblPeriod;
8 private JLabel jlblDeparture, jlblDays, jlblZone, jlblPlan;
9 private JTextField jtfName, jtfIC, jtfAdd, jtfPostal, jtfContact, jtfEmail, jtfZone;
10 private JRadioButton jrbResident, jrbOffice, jrbDeluxe, jrbClassic, jrbAsia, jrbWorldwide;
11 private ButtonGroup bgContact, bgZone, bgPlan;
12 private JComboBox jcDay, jcMonth, jcYear;
13 private JButton jbtnSubmit, jbtnCalculate, jbtnClear;
14 private JPanel jpTitle,jp1, jp2, jp3, jp4, jp5, jp6, jp7, jpBottom, jpCenterArea;
15 String[] day = {"1", "2", "3"};
16 String[] month = {"january", "february"};
17 String[] year = {"1981", "1985", "1990", "1995"};
18
19 //constructor and GUI development
20 public TravelInsuranceApplication()
21 {
22 setSize(500,200);
23 setTitle("Travel Insurance Application");
24 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25 setLayout(new BorderLayout());
26
27 //create ALL component objects/
28 jlblTitle = new JLabel("Travel Insurance Application: ");
29 jlblName = new JLabel("Name of Insured: ");
30 jlblNRIC = new JLabel("NRIC: ");
31 jlblAdd = new JLabel("Address: ");
32 jlblPostal = new JLabel("Postal Code: ");
33 jlblContact = new JLabel("Telephone: ");
34 jlblDOB = new JLabel("Date Of Birth: ");
35 jlblEmail = new JLabel("Email Address: ");
36 jlblPeriod = new JLabel("Period Of Insurance ");
37 jlblDeparture = new JLabel("Departure Date ");
38 jlblDays = new JLabel("How Many Days To Insure ");
39 jlblZone = new JLabel("Zone: ");
40 jlblPlan = new JLabel("Plan: ");
41
42 jtfName = new JTextField(50);
43 jtfIC = new JTextField(15);
44 jtfAdd = new JTextField(50);
45 jtfPostal = new JTextField(15);
46 jtfContact = new JTextField(15);
47 jtfEmail = new JTextField(50);
48 jtfZone = new JTextField(100);
49
50 jrbResident = new JRadioButton("Rseident/Pgr");
51 jrbOffice = new JRadioButton("Office/HP");
52 jrbAsia = new JRadioButton("Asia");
53 jrbAsia = new JRadioButton("Worldwide");
54 jrbDeluxe = new JRadioButton("Deluxe");
55 jrbClassic = new JRadioButton("Classic");
56
57 jcDay = new JComboBox(day);
58 jcMonth = new JComboBox(month);
59 jcYear = new JComboBox(year);
60
61 jbtnSubmit = new JButton("Submit");
62 jbtnCalculate = new JButton("Calculate");
63 jbtnClear = new JButton("Clear");
64
65 /****create JPanels - jpTitle, JpCenterArea & jp2-jp8 , jpBottom + setLayout 66 for ALL JPanels******/
67 jpTitle = new JPanel(new FlowLayout(FlowLayout.CENTER));
68 jpCenterArea = new JPanel(new FlowLayout());
69 jp1 = new JPanel(new FlowLayout());
70 jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
71 jp3 = new JPanel(new FlowLayout());
72 jp4 = new JPanel(new FlowLayout());
73 jp5 = new JPanel(new FlowLayout());
74 jp6 = new JPanel(new FlowLayout(FlowLayout.CENTER));
75 jp7 = new JPanel(new FlowLayout(FlowLayout.CENTER));
76 jpBottom = new JPanel(new FlowLayout(FlowLayout.CENTER));
77
78
79
80
81 //add components to JPanels
82 jpTitle.add(jlblTitle);
83
84 //jp1
85 jp1.add(jlblName);
86 jp1.add(jtfName);
87 jp1.add(jlblNRIC);
88 jp1.add(jtfIC);
89
90 //jp2
91 jp2.add(jlblAdd);
92 jp2.add(jtfAdd);
93 jp2.add(jlblPostal);
94 jp2.add(jtfPostal);
95
96 //jp3
97 jp3.add(jlblContact);
98 jp3.add(jtfContact);
99 jp3.add(jrbResident);
100 jp3.add(jrbOffice);
101 jp3.add(jlblDOB);
102 jp3.add(jcDay);
103 jp3.add(jcMonth);
104 jp3.add(jcYear);
105
106 //jp4
107 jp4.add(jlblEmail);
108 jp4.add(jtfEmail);
109
110 //jp5
111 jp5.add(jlblPeriod);
112 jp5.add(jlblDeparture);
113 jp5.add(jcDay);
114 jp5.add(jcMonth);
115 jp5.add(jcYear);
116 jp5.add(jlblDays);
117 jp5.add(jcDay);
118
119 //jp6
120 jp6.add(jlblZone);
121 jp6.add(jrbAsia);
122 jp6.add(jrbWorldwide);
123 jp6.add(jlblPlan);
124 jp6.add(jrbDeluxe);
125 jp6.add(jrbClassic);
126
127 //jp7
128 jp7.add(jtfZone);
129
130 //jpCenterArea to hold jp1-jp7
131 jpCenterArea.add(jp1);
132 jpCenterArea.add(jp2);
133 jpCenterArea.add(jp3);
134 jpCenterArea.add(jp4);
135 jpCenterArea.add(jp5);
136 jpCenterArea.add(jp6);
137 jpCenterArea.add(jp7);
138
139 //jpBottom
140 jpBottom.add(jbtnSubmit);
141 jpBottom.add(jbtnCalculate);
142 jpBottom.add(jbtnClear);
143
144 /********Add Jpanels to JFrame*****/
145 add(jpTitle, BorderLayout.NORTH);
146 add(jpCenterArea, BorderLayout.CENTER);
147 add(jpBottom, BorderLayout.SOUTH);
148
149 setVisible(true);
150
151
152
153 }//end null constructor
154 public static void main(String[] args)
155 {
156 TravelInsuranceApplication travel = new TravelInsuranceApplication();
157
158 }//end main
159
160 }//end class