Results 1 to 8 of 8
- 12-01-2010, 10:21 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
1 of 8 global variables not visible
I am writing a fairly simple program in the attempt to teach myself JAVA. I have 8 global variables, 3 JComboBoxes, 3 Calendar.instances, 1 int, and 1 JTextArea declared. All have been initialized. My ActionListener routine that services the JComboBoxes accesses or tries to access all of the global variables. It is successful on 7 of them, but I get a NULL reference pointer error at runtime on the JTextArea access. What am I doing wrong?
TIA,
Bobbo
-
1) You've got a bug in your code.What am I doing wrong?
2) You're assuming that we can guess what that bug is without seeing code or knowing which line throws the exception.
-
But seriously... show us the code and the exception, and the line that throw the exception, and we'll likely be able to help you rather than guess.
Much luck!
- 12-01-2010, 11:29 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
1 of 8 global variables not visible
LINK REMOVED BY BOBBO
is a link to the source file. I am declaring the JTextArea variable jta on line 13. I initialize the jta variable on line 72. I am getting the error message at runtime on line 169. (It is currently commented out so it will run.) The error message is
Exception in thread "main" java.lang.NullPointerException
at working.Working.actionPerformed(Working.java:161)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at working.Working.run(Working.java:64)
at working.Working.main(Working.java:22)
The actual line giving the error is
jta.setText(result);
which puts the String variable result into the JTextArea for display.Last edited by Bobbo; 12-06-2010 at 06:00 AM.
-
Best to post your code here, not at a second site. Here is your code.
Java Code:import java.awt.FlowLayout; import java.awt.event.*; import java.util.Calendar; import javax.swing.*; public class Working implements ActionListener { JComboBox mcb; JComboBox ycb; JComboBox dcb; JTextArea jta; JScrollPane sP; Calendar now; Calendar later; Calendar permanent; int flag = 1; public static void main(String[] args) { Working me = new Working(); me.run(); } public void run() { now = Calendar.getInstance(); later = Calendar.getInstance(); permanent = Calendar.getInstance(); permanent.set(2010, 10, 23); JFrame jf = new JFrame("Working"); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setSize(640, 480); jf.setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS)); JLabel ml = new JLabel("Month"); String[] Months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; mcb = new JComboBox(Months); mcb.setName("Month"); mcb.setSelectedIndex(now.get(Calendar.MONTH)); mcb.addActionListener(this); JPanel mp = new JPanel(); mp.setLayout(new BoxLayout(mp, BoxLayout.Y_AXIS)); mp.add(ml); mp.add(mcb); JLabel yl = new JLabel("Year"); ycb = new JComboBox(setYear(now.get(Calendar.YEAR))); ycb.setName("Year"); ycb.setSelectedItem(((Integer) (now.get(Calendar.YEAR))).toString()); ycb.addActionListener(this); JPanel yp = new JPanel(); yp.setLayout(new BoxLayout(yp, BoxLayout.Y_AXIS)); yp.add(yl); yp.add(ycb); JLabel dl = new JLabel("Day"); dcb = new JComboBox(setDay()); dcb.setName("Day"); dcb.addActionListener(this); flag = 0; dcb.setSelectedIndex(now.get(Calendar.DAY_OF_MONTH) - 1); JPanel dp = new JPanel(); dp.setLayout(new BoxLayout(dp, BoxLayout.Y_AXIS)); dp.add(dl); dp.add(dcb); jta = new JTextArea(2, 20); jta.setEditable(false); sP = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); JPanel fp = new JPanel(); fp.setLayout(new FlowLayout()); fp.add(mp); fp.add(dp); fp.add(yp); jf.add(fp); jf.add(sP); // jf.pack(); jf.setVisible(true); } private void fillYear(int yr) { String[] local = setYear(yr); flag++; ycb.removeAllItems(); for (int i = 0; i < local.length; i++) { ycb.addItem(local[i]); } ycb.setSelectedIndex(1); flag--; } private String[] setYear(int y) { y = (y - (y % 10)) - 1; String[] local = new String[12]; local[0] = "Lower"; local[11] = "Higher"; for (int i = 1; i < 11; i++) { local[i] = ((Integer) (y + i)).toString(); } return local; } private void fillDay() { flag++; int a = dcb.getSelectedIndex(); dcb.removeAllItems(); String[] local = setDay(); for (int i = 0; i < local.length; i++) { dcb.addItem(local[i]); } if (a >= local.length) a = local.length - 1; flag--; dcb.setSelectedIndex(a); } private String[] setDay() { final Integer[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String[] local; int d = month[mcb.getSelectedIndex()] + LY(); local = new String[d]; for (int i = 0; i < d; i++) { local[i] = ((Integer) (i + 1)).toString(); } return local; } private int LY() { if (mcb.getSelectedIndex() == 1) { int lly = Integer.parseInt(((String) ycb.getSelectedItem())); if ((lly % 400) == 0) return 1; if ((lly % 100) == 0) return 0; if ((lly % 4) == 0) return 1; } return 0; } public void actionPerformed(ActionEvent e) { String result; String[] dow = {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday"}; JComboBox JCB = (JComboBox) e.getSource(); if (JCB.getName() == "Day") { if (flag == 0) { later.set(Integer.parseInt((String) ycb.getSelectedItem()), mcb.getSelectedIndex(), dcb .getSelectedIndex() + 1); long count = (later.getTimeInMillis() - permanent.getTimeInMillis()) / 86400000; count = ((count % 14) + 14) % 14; switch ((int) count) { case 0: ; case 1: ; case 2: ; case 3: ; case 4: ; case 5: ; case 6: result = "Bob is working."; break; default: result = "Bob is off."; break; } jta.setText(result); result = "Today is " + dow[(int) count] + "."; System.out.println(result); } return; } if (JCB.getName() == "Year") { int b = ycb.getSelectedIndex(); switch (b) { case 0: { int i = Integer.parseInt((String) ycb.getItemAt(1)) - 10; fillYear(i); break; } case 11: int i = Integer.parseInt((String) ycb.getItemAt(1)) + 10; fillYear(i); break; } fillDay(); } if (JCB.getName() == "Month") { fillDay(); } } }
Your problem is that you're adding ActionListeners to non-rendered components, and then triggering the ActionListeners with this line:
but since the components have not been rendered by setVisible(true) or pack, a NullPointerException will occur when you try to set the text in the null JTextArea. The solution is add the listeners after the line of code above like so:Java Code:dcb.setSelectedIndex(now.get(Calendar.DAY_OF_MONTH) - 1);
Java Code:mcb.setSelectedIndex(now.get(Calendar.MONTH)); //!! mcb.addActionListener(this); JPanel mp = new JPanel(); mp.setLayout(new BoxLayout(mp, BoxLayout.Y_AXIS)); mp.add(ml); mp.add(mcb); JLabel yl = new JLabel("Year"); ycb = new JComboBox(setYear(now.get(Calendar.YEAR))); ycb.setName("Year"); ycb.setSelectedItem(((Integer) (now.get(Calendar.YEAR))).toString()); //!! ycb.addActionListener(this); JPanel yp = new JPanel(); yp.setLayout(new BoxLayout(yp, BoxLayout.Y_AXIS)); yp.add(yl); yp.add(ycb); JLabel dl = new JLabel("Day"); dcb = new JComboBox(setDay()); dcb.setName("Day"); //!! dcb.addActionListener(this); flag = 0; dcb.setSelectedIndex(now.get(Calendar.DAY_OF_MONTH) - 1); //!! mcb.addActionListener(this); ycb.addActionListener(this); dcb.addActionListener(this); //!!
- 12-02-2010, 03:36 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
1 of 8 global variables not visible
I have studied that code for days, and could have studied it for weeks without finding that. You are a genius. That fixed the problem.
Thanks, Bobbo :)
-
- 12-02-2010, 05:07 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
1 of 8 global variables not visible
Actually, I had placed that addActionListener statement prior to the dcb.setIndex() statement intentionally so the frame would open with the TextArea populated with that day's information. What I did to fix it was to move the instantiation of the JTextArea to the front of the routine so its pointer would no longer be NULL.
Bobbo
Similar Threads
-
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
global variables
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-08-2009, 07:19 PM -
Declaring global variables
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks