Results 1 to 4 of 4
  1. #1
    Stormageddon is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default NullPointerException

    Hi all. I'm currently writing a program to display a swing user interface for a 2012 calendar. The source code compiles, but when I try to run it, it gives me a Null Pointer Exception. Any ideas on what's wrong?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CalendarApp extends JFrame{
    	String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    	String[] daysWeek = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
    	public CalendarApp(){
    		super("2012");
    		this.setSize(1000,1000);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//sets up headings for the months and adds them to a list to be used individually later
    		JPanel[] monthLabelList = new JPanel[12];
    		for (int i = 0; i < 12; i++){
    			JPanel monthHeadingPane = new JPanel();
    			FlowLayout monthHeadingLayout = new FlowLayout();
    			monthHeadingPane.setLayout(monthHeadingLayout);
    			JLabel monthHeading = new JLabel(months[i]);
    			monthHeadingPane.add(monthHeading);
    			monthLabelList[i] = monthHeadingPane;
    		}
    
    		//sets up pane with a box layout for headings of the days of the week
    		JPanel daysWeekPane = new JPanel();
    		BoxLayout daysWeekLayout = new BoxLayout(daysWeekPane, BoxLayout.X_AXIS);
    		daysWeekPane.setLayout(daysWeekLayout);
    		for (int j = 0; j < 7; j++){
    			JLabel dayWeek = new JLabel(daysWeek[j]);
    			daysWeekPane.add(dayWeek);
    			}
    
    		//sets up the display of dates for each month and adds to a list to be used later
    		JPanel[] monthCalendarList = new JPanel[12];
    		for (int k = 0; k < 12; k++){
    			JPanel monthCalendarPane = new JPanel();
    			GridLayout monthCalendarLayout = new GridLayout(6, 7);
    			monthCalendarPane.setLayout(monthCalendarLayout);
    
    			if (months[k] == "January"){
    					for (int m = 1; m <= 31; m++){
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 11; m++){
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "February")
    				{
    					for (int m = 1; m <= 3; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 29; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 10; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "March")
    				{
    					for (int m = 1; m <= 4; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 7; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "April")
    				{
    					for (int m = 1; m <= 30; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 12; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "May")
    				{
    					for (int m = 1; m <= 2; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 9; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "June")
    				{
    					for (int m = 1; m <= 5; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 30; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 7; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "July")
    				{
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 11; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "August")
    				{
    					for (int m = 1; m <= 3; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 8; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "September")
    				{
    					for (int m = 1; m <= 6; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 30; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 6; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "October")
    				{
    					for (int m = 1; m <= 1; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 10; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "November")
    				{
    					for (int m = 1; m <= 4; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 30; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 8; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    
    			else if (months[k] == "December")
    				{
    					for (int m = 1; m <= 6; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 31; m++)
    					{
    						JLabel day = new JLabel(" " + m);
    						monthCalendarPane.add(day);
    					}
    					for (int m = 1; m <= 5; m++)
    					{
    						JLabel day = new JLabel(" ");
    						monthCalendarPane.add(day);
    					}
    				}
    			
    
    		monthCalendarList[k] = monthCalendarPane;
    		}
    
    		//combines month heading, days of the week,and dates into one pane for each month
    		JPanel[] monthDisplayList = new JPanel[12];
    		for (int n = 0; n < 12; n++){
    			JPanel monthDisplayPane = new JPanel();
    			BoxLayout monthDisplayLayout = new BoxLayout(monthDisplayPane, BoxLayout.Y_AXIS);
    			monthDisplayPane.setLayout(monthDisplayLayout);
    			monthDisplayPane.add(monthLabelList[n]);
    			monthDisplayPane.add(daysWeekPane);
    			monthDisplayPane.add(monthCalendarList[n]);
    			monthDisplayPane = monthDisplayList[n];
    			}
    
    		//combines panes for every month to build the calendar
    		JPanel calendarDisplayPane = new JPanel();
    		GridLayout calendarLayout = new GridLayout(4, 3, 10, 10);
    		calendarDisplayPane.setLayout(calendarLayout);
    		for (int o = 0; o < 12; o++) \\exception seems to occur here according to error message
    			calendarDisplayPane.add(monthDisplayList[o]);
    			
    		add(calendarDisplayPane);
    		setVisible(true);
    		}
    	//main method
    	public static void main(String[] arguments){
    		CalendarFrame calendar = new CalendarFrame();
    		
    	}
    }
    Last edited by Fubarable; 07-27-2012 at 02:24 AM. Reason: code tags added

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: NullPointerException

    Can you please post the output that you receive from the command line upon running the program?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: NullPointerException

    Hi Stormaggedon, welcome to the forums!

    Could you edit your post to add "code" tags? Put [code] at the start of a section of code and [/code] at the end - that way the post is marked correctly by the forum software and will be readable.

    Also when you get a runtime exception like a NullPointerException, post the full stack trace and indicate which line(s) of your code it is referring to.

    -----

    I don't know if it's related to the exception, but don't compare strings (or other objects) with ==, use the equals method instead. == means something more like "is identical to" and you are unlikely to men that. Every class implements equals() to represent "is equal to". Think citizens: we are equal, yet distinct. In fact if there were only one of us (if we were identical) our equality wouldn't mean much.

    Java Code:
    String foo = ...;
        // no - this condition is too "strong"
    //if(foo == "whatever") {
    if(foo.equals("whatever")){

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: NullPointerException

    Quote Originally Posted by Stormageddon View Post
    Java Code:
    //combines month heading, days of the week,and dates into one pane for each month
    		JPanel[] monthDisplayList = new JPanel[12];
    		for (int n = 0; n < 12; n++){
    			JPanel monthDisplayPane = new JPanel();
    			BoxLayout monthDisplayLayout = new BoxLayout(monthDisplayPane, BoxLayout.Y_AXIS);
    			monthDisplayPane.setLayout(monthDisplayLayout);
    			monthDisplayPane.add(monthLabelList[n]);
    			monthDisplayPane.add(daysWeekPane);
    			monthDisplayPane.add(monthCalendarList[n]);
    			monthDisplayPane = monthDisplayList[n];
    			}
    Also, something looks wrong here. monthDisplayList[n] is still null on the final line of your for-loop. I think what you meant to have as the final line of your for-loop is this:

    Java Code:
    monthDisplayList[n] = monthDisplayPane;
    However, at this point monthDisplayList is full of 12 null values, and that is why this for-loop throws a null-pointer exception:

    Quote Originally Posted by Stormageddon View Post
    Java Code:
    for (int o = 0; o < 12; o++) \\exception seems to occur here according to error message
                calendarDisplayPane.add(monthDisplayList[o]);
    You can't add null to a JPanel().
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

Similar Threads

  1. nullpointerexception
    By natdizzle in forum New To Java
    Replies: 3
    Last Post: 01-14-2012, 09:57 PM
  2. NullPointerException help
    By laichunkitjacky in forum New To Java
    Replies: 4
    Last Post: 12-04-2011, 05:20 PM
  3. NullPointerexception
    By danewithoutwax in forum New To Java
    Replies: 4
    Last Post: 11-26-2011, 12:51 PM
  4. NullPointerException
    By speedzojie@gmail.com in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 05:39 PM
  5. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 10:51 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
  •