Results 1 to 8 of 8
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default Problem creating arrays

    Hello,

    I am creating a registration form and I am building the JComboBox for the date of birth. I am getting an error telling me to delete by semicolon at the end of the array for date of birth but not on the array for the years. I have created both arrays in the same way. If I delete this semi colon I get a bunch more errors.
    The code is outside of a constructor and the error message I get is >> Syntax error on token ";", { expected after this token

    I have placed comments in the code where the error takes place,
    Here is the code:
    Java Code:
    //Date of Birth
    private JLabel dob = new JLabel("Date of Birth:");
    //Days
    private String[] days = {}; //ERROR, delete semicolon???
    for(int d=1;d<=31;d++){
    	days[d]=Integer.toString(d);
    }
    private JComboBox day = new JComboBox(days);
    //Months
    private String[] months = {"January","February","March","April","May","June",
    		"July","August","September","October","November","December"};
    private JComboBox month = new JComboBox(months);
    //Years
    private String years[] = {}; //NOERROR????
    for(int y=1950;y<=2012;y++){
    	years[y]=Integer.toString(y);
    }
    private JComboBox year = new JComboBox(years);
    Last edited by JohnPringle83; 05-06-2011 at 07:41 PM.

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

    Default

    Please fix your code tags and show the actual error message itself rather than paraphrasing it. Also where is this code? Is it in a method or constructor? If so, your variables shouldn't have access modifiers such as private. Or is out in the class? If so, then you can't do programming statements other than variable declarations and initializations.

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

    Default

    Quote Originally Posted by JohnPringle83 View Post
    The code is outside of a constructor and the error message I get is >> Syntax error on token ";", { expected after this token
    The problem is here:
    Java Code:
    for(int d=1;d<=31;d++){
    	days[d]=Integer.toString(d);
    }
    You can't have programming statements like this other than variable declarations with or without initializations outside of a method or constructor (we'll ignore the other possible blocks for now). In other words, put this code in the constructor.

    This should be well described in your textbook or tutorial. Please have a look at it again.

  4. #4
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    I tried what you said but now the application reels of loads of errors when I try to run it;

    here is the code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class RegScreen extends JFrame{
    	
    	private JLabel please = new JLabel("Please complete all parts of the form");
    	private JLabel tl = new JLabel("Title:");
    	private String[] titles = {"Mr", "Mrs", "Miss", "Ms"};
    	private JComboBox title = new JComboBox(titles);
    	private JLabel fnl = new JLabel("First Name:");
    	private JTextField fn = new JTextField(20);
    	private JLabel lnl = new JLabel("Last Name");
    	private JTextField ln = new JTextField(20);
    	private JLabel el = new JLabel("Email:");
    	private JTextField email = new JTextField(20);
    	//Date of Birth
    	private JLabel dob = new JLabel("Date of Birth:");
    	private String[] days;
    	private JComboBox day = new JComboBox(days);
    	private String[] months = {"January","February","March","April","May","June", 
    			"July","August","September","October","November","December"};
    	private JComboBox month = new JComboBox(months);
    	private String years[];
    	private JComboBox year = new JComboBox(years);
    
    	public RegScreen(){
    		super("Register");
    		setLayout(new FlowLayout(FlowLayout.LEFT));
    		
    		for(int d=1;d<=31;d++){
    			days[d]=Integer.toString(d);
    		}
    		for(int y=1950;y<=2012;y++){
    			years[y]=Integer.toString(y);
    		}
    		
    		add(please);
    		add(tl);
    		add(title);
    		add(fnl);
    		add(fn);
    		add(lnl);
    		add(ln);
    		add(el);
    		add(email);
    		add(dob);
    		add(day);
    		add(month);
    		add(year);
    		
    	}
    }
    Here are the errors in the console:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at javax.swing.DefaultComboBoxModel.<init>(Unknown Source)
    	at javax.swing.JComboBox.<init>(Unknown Source)
    	at RegScreen.<init>(RegScreen.java:21)
    	at StartScreen$2.actionPerformed(StartScreen.java:34)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

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

    Default

    This error is due to something completely different. If you have a NPE, you need to find out which line is causing the error and then inspect your code at that line, find out which object is null and then find out why. Often once you inspect it, the cause is obvious. Please come back if still stumped (and tell us which line is causing this error!).

  6. #6
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Your problem is here:
    Java Code:
    	private String years[];
    	private JComboBox year = new JComboBox(years);
    years is null, so when you create your JComboBox, you're passing in null for the values. You need to not initialize your JComboBox until later in your constructor, after your years array has been initialized.

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

    Default

    Quote Originally Posted by snotmare View Post
    Your problem is here:
    Java Code:
    	private String years[];
    	private JComboBox year = new JComboBox(years);
    years is null, so when you create your JComboBox, you're passing in null for the values. You need to not initialize your JComboBox until later in your constructor, after your years array has been initialized.
    You are of course correct. I was trying to nudge the OP into seeing this for himself. :)

  8. #8
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    I did work it out eventually when I looked at the error message and it was a NP error. I guessed it meant that the code was pointing to something that was null. So I looked at why it might be null and realised that I was trying to feed into the combo box an array that had no values so, Instead of using a loop to create the array elements in the constructor, I decided to create the array manually before initializing the combobox. I've still got a lot to learn but step by step, I'm getting there. :)

Similar Threads

  1. Problem with arrays
    By Viper in forum New To Java
    Replies: 7
    Last Post: 10-07-2010, 03:49 PM
  2. Creating generic arrays
    By stijn1989 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2009, 02:26 PM
  3. Creating a constructor with arrays and arguments - Part 2
    By fullmetaljacket in forum New To Java
    Replies: 15
    Last Post: 07-05-2009, 02:38 PM
  4. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 07:09 AM
  5. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 07:01 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
  •