Results 1 to 5 of 5
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    I just noticed it - it was because i hadn't imported FlowLayout().
    Thanks alot for your help :) !

Similar Threads

  1. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  2. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 07:16 AM
  3. Null Pointer Exception
    By musasabi in forum New To Java
    Replies: 3
    Last Post: 05-12-2010, 03:52 AM
  4. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  5. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •