Results 1 to 8 of 8
Thread: Problem creating arrays
- 05-06-2011, 06:35 PM #1
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
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 06:41 PM.
-
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.
-
The problem is here:
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.Java Code:for(int d=1;d<=31;d++){ days[d]=Integer.toString(d); }
This should be well described in your textbook or tutorial. Please have a look at it again.
- 05-06-2011, 07:18 PM #4
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
I tried what you said but now the application reels of loads of errors when I try to run it;
here is the code:
Here are the errors in the console: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); } }
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)
-
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!).
- 05-06-2011, 09:11 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
Your problem is here:
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.Java Code:private String years[]; private JComboBox year = new JComboBox(years);
-
- 05-06-2011, 11:01 PM #8
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
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
-
Problem with arrays
By Viper in forum New To JavaReplies: 7Last Post: 10-07-2010, 02:49 PM -
Creating generic arrays
By stijn1989 in forum Advanced JavaReplies: 6Last Post: 11-18-2009, 01:26 PM -
Creating a constructor with arrays and arguments - Part 2
By fullmetaljacket in forum New To JavaReplies: 15Last Post: 07-05-2009, 01:38 PM -
Creating a constructor with arrays and arguments
By fullmetaljacket in forum New To JavaReplies: 38Last Post: 07-03-2009, 06:09 AM -
[SOLVED] Creating an Array of Arrays?
By xcallmejudasx in forum Advanced JavaReplies: 5Last Post: 11-04-2008, 06:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks