Results 1 to 5 of 5
- 04-02-2011, 04:17 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Null Pointer Exception in an Array of JCheckBoxes
So I have an array of 7 checkboxes with the names of consecutive days next to them. The program checks off the days how many consecutive days the program was run.
I get a null pointer exception when i initialize the array of JCheckBoxes (in REd below). I'm noe sure how to fix it. Could use a little help and explanation of what I did wrong, thanks!!
Java Code:import java.util.Calendar; import java.util.Formatter; import java.util.Scanner; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JOptionPane; public class ConsecutiveLaunch extends JFrame{ [B]private JCheckBox[] launchCheckBox = new JCheckBox[6]; private int today; private int consecutiveLaunchesNumber; private int yesterday;[/B] [B]private void getToday()[/B] { //use calendar function to get numerical value of today and stores it in this.today } [B]public int getDayAfterToday(int daysAfterToday)[/B] { //returns the numerical value of the day = today+daysAfterToday. } [B]public String toString(int day)[/B] { //converts the numerical values of day into a string e.g. 2 = "Monday" } [B]private void readLaunchRecords()[/B] { //reads the last day of launch and consecutiveLaunchesNumber //sets this.yesterday = last day of launch //sets this.consecutiveLaunchesNumber = consecutiveLaunchesNumber from file. } [B]private void writeLaunchRecords()[/B] { //writes this.today and update consecutiveLaunchesNumber onto file. } [B]private boolean isLaunchConsecutive()[/B] { //checks to see if launches are consecutive } [B]private void displayConsecutiveLaunches()[/B] { for(int index=0;index<=this.consecutiveLaunchesNumber;index++) { launchCheckBox[index].setSelected(true); } } [B]private void createLaunchRecords()[/B] { //writes launch Records if they don't already exist } [B]public ConsecutiveLaunch()[/B] { super("Consecutive Launch Bonus"); this.getToday(); readLaunchRecords(); if(isLaunchConsecutive()==true) { this.consecutiveLaunchesNumber++; }else { this.consecutiveLaunchesNumber=1; } writeLaunchRecords(); for(int index=0;index<launchCheckBox.length;index++) { [COLOR="Red"][B]launchCheckBox[index] = new JCheckBox(toString(getDayAfterToday(index++)));//NULL POINTER EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!![/B][/COLOR] launchCheckBox[index].setEnabled(false); add(launchCheckBox[index]); } displayConsecutiveLaunches(); } }Last edited by eLancaster; 04-02-2011 at 04:36 PM. Reason: Post isn't solved
-
You should avoid trying to mess with a for loops index from within the for loop itself.
Java Code:for(int index=0;index<launchCheckBox.length;index++) { launchCheckBox[index] = new JCheckBox(toString(getDayAfterToday(index++)));
In the block of code above you have index++, but the index is already being incremented by the for loop itself, so you will mess up the loop by doing this.
- 04-02-2011, 04:33 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Yeah, I realized that and then fixed it and that problem got solved.
But I have another issue - when I run the program, it only shows one day "Thursday" and marks it unchecked. I don't understand why it only shows one day. I've worked through the code and it should show all 7 checkboxes with their respective days.
Could you help me out with that
P.S. sorry for marking the thread as solved when it isn't ...yet.
-
Can't help you yet, as your code is incomplete -- you have method bodies that you have left blank.
- 04-02-2011, 04:50 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Similar Threads
-
Null Pointer exception (Again !!)
By mobosecomin in forum New To JavaReplies: 6Last Post: 03-29-2011, 05:04 PM -
Null Pointer Exception
By jonytek in forum New To JavaReplies: 5Last Post: 03-02-2011, 07:16 AM -
Null Pointer Exception
By musasabi in forum New To JavaReplies: 3Last Post: 05-12-2010, 03:52 AM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 02:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks