 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
justin Guest
|
Posted: Fri Apr 23, 2004 12:19 pm Post subject: Array Creation |
|
|
Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
|
|
| Back to top |
|
 |
Christophe Vanfleteren Guest
|
Posted: Fri Apr 23, 2004 12:58 pm Post subject: Re: Array Creation |
|
|
justin wrote:
| Quote: | Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
|
To create your array of Swimmers:
Swimmer[] swimmers = new Swimmer[10];
to put an actual swimmer in the array:
swimmers[0] = new Swimmer("myname",...);
In your GUI class, add 2 buttons(previous and next) and a textfield
..
Also keep an index around, that will tell you which swimmer of the array you
are showing:
public class GUIClass ... {
private int currentIndex = 0;
private Swimmer[] swimmers;
public GUICLass() {
//create and fill the array of swimmers
...
//and show first swimmer
showSwimmer(0);
}
public showSwimmer(int index) {
txtField.setText(swimmers[currentIndex].toString());
}
}
Now you have to add listeners to your buttons to react to them being clicked
on:
nextButton.addActionListener(new ActionListener() {
public void actionPërformed(ActionEvent evt) {
currentIndex++;
showSwimmer(currentIndex);
}
});
ps. I (as in, I am), is spelled with a capital I in English.
--
Kind regards,
Christophe Vanfleteren
|
|
| Back to top |
|
 |
justin Guest
|
Posted: Fri Apr 23, 2004 11:39 pm Post subject: Re: Array Creation |
|
|
Thank You Christophe, ahha with the I was just trying to type
faster..i have tried implementing as much you have taught me and so
far this is what I have done.
class SwimmerGUI extends JFrame implements ActionListener{
private int currentIndex = 0;
private Swimmer[] swimmers;
public SwimmerGUI(){
Swimmer[] swimmers = new Swimmer[10];
swimmers[0] = new Swimmer("Jeremy","Michaels","Male",65,172,"Butterfly","1.03",1983,6, ;
the thing is with the textfields, there's is more than jsut one text
field so basically I am trying to display name in nameDisplayField ,
surname in surnameDisplayField etc...how do i do this? thanks
Christophe Vanfleteren <c.v4nfl3t3r3n (AT) pandora (DOT) be> wrote
| Quote: | justin wrote:
Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
To create your array of Swimmers:
Swimmer[] swimmers = new Swimmer[10];
to put an actual swimmer in the array:
swimmers[0] = new Swimmer("myname",...);
In your GUI class, add 2 buttons(previous and next) and a textfield
.
Also keep an index around, that will tell you which swimmer of the array you
are showing:
public class GUIClass ... {
private int currentIndex = 0;
private Swimmer[] swimmers;
public GUICLass() {
//create and fill the array of swimmers
...
//and show first swimmer
showSwimmer(0);
}
public showSwimmer(int index) {
txtField.setText(swimmers[currentIndex].toString());
}
}
Now you have to add listeners to your buttons to react to them being clicked
on:
nextButton.addActionListener(new ActionListener() {
public void actionPërformed(ActionEvent evt) {
currentIndex++;
showSwimmer(currentIndex);
}
});
ps. I (as in, I am), is spelled with a capital I in English.
|
|
|
| Back to top |
|
 |
justin Guest
|
Posted: Fri Apr 23, 2004 11:54 pm Post subject: Re: Array Creation |
|
|
Christophe, i got this error when clicking the next button. What I
have done I omitted all the other attributes of the class swimmer
except for name and so the only element I have for my swimmer array is
name of object swimmer in index 0.I did this just to test if the name
would display in the nameDisplayField but on running the program and
clicking the next button I got the following:
C:JBuilderXjdk1.4binjavaw -classpath "C:Documents and
SettingsJustinjbprojectMySwimmerApplicationclasses;C:JBuilderXjdk1.4demojfcJava2DJava2Demo.jar;C:JBuilderXjdk1.4demopluginjfcJava2DJava2Demo.jar;C:JBuilderXjdk1.4jrejavawsjavaws.jar;C:JBuilderXjdk1.4jrelibcharsets.jar;C:JBuilderXjdk1.4jrelibextdnsns.jar;C:JBuilderXjdk1.4jrelibextldapsec.jar;C:JBuilderXjdk1.4jrelibextlocaledata.jar;C:JBuilderXjdk1.4jr
libextsunjce_provider.jar;C:JBuilderXjdk1.4jrelibimindicim.jar;C:JBuilderXjdk1.4jrelibimthaiim.jar;C:JBuilderXjdk1.4jrelibjce.jar;C:JBuilderXjdk1.4jrelibjsse.jar;C:JBuilderXjdk1.4jrelibplugin.jar;C:JBuilderXjdk1.4jrelibrt.jar;C:JBuilderXjdk1.4jrelibsunrsasign.jar;C:JBuilderXjdk1.4libdt.jar;C:JBuilderXjdk1.4libhtmlconverter.jar;C:JBuilderXjdk1.4lib
tools.jar" myswimmerapplication.SwimmerGUI
java.lang.NullPointerException
at myswimmerapplication.SwimmerGUI.showSwimmer(SwimmerGUI.java:220)
at myswimmerapplication.SwimmerGUI.actionPerformed(SwimmerGUI.java:214)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
my code for the nextButton is as follows
public void actionPerformed(ActionEvent event){
if (event.getSource() instanceof JButton){
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == exitButton){
System.exit(0);
}
else if (clickedButton == nextButton){
currentIndex++;
showSwimmer(currentIndex);
}
}
}
public void showSwimmer(int index){
nameDisplayField.setText(swimmers[currentIndex].toString());
}
and in this case my swimmer array is now this :
Swimmer[] swimmers = new Swimmer[10];
swimmers[0] = new Swimmer("Jeremey");
what went wrong? Anyone would like to help me out here as well? Thanks
Christophe Vanfleteren <c.v4nfl3t3r3n (AT) pandora (DOT) be> wrote
| Quote: | justin wrote:
Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
To create your array of Swimmers:
Swimmer[] swimmers = new Swimmer[10];
to put an actual swimmer in the array:
swimmers[0] = new Swimmer("myname",...);
In your GUI class, add 2 buttons(previous and next) and a textfield
.
Also keep an index around, that will tell you which swimmer of the array you
are showing:
public class GUIClass ... {
private int currentIndex = 0;
private Swimmer[] swimmers;
public GUICLass() {
//create and fill the array of swimmers
...
//and show first swimmer
showSwimmer(0);
}
public showSwimmer(int index) {
txtField.setText(swimmers[currentIndex].toString());
}
}
Now you have to add listeners to your buttons to react to them being clicked
on:
nextButton.addActionListener(new ActionListener() {
public void actionPërformed(ActionEvent evt) {
currentIndex++;
showSwimmer(currentIndex);
}
});
ps. I (as in, I am), is spelled with a capital I in English.
|
|
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Sat Apr 24, 2004 12:38 am Post subject: Re: Array Creation |
|
|
"justin" wrote...
The error you're getting tells you what's wrong:
| Quote: | java.lang.NullPointerException
|
Somewhere in your code you're using a variable that doesn't reference any
object.
This happens at row 220 in your code:
| Quote: | at myswimmerapplication.SwimmerGUI.showSwimmer
(SwimmerGUI.java:220)
|
....the method showSwimmer is in turn called at row 214, etc...
| Quote: | at myswimmerapplication.SwimmerGUI.actionPerformed
(SwimmerGUI.java:214)
|
[snip]
| Quote: | public void actionPerformed(ActionEvent event)
{
if (event.getSource() instanceof JButton)
{
JButton clickedButton =
(JButton) event.getSource();
if (clickedButton == exitButton)
{
System.exit(0);
}
else if (clickedButton == nextButton)
{
currentIndex++;
|
Here's one dangerous line!
You don't check that "currentIndex" exceeds the
allowed value for the index in your array...
If you have 10 Swimmers you could e.g. use:
currentIndex = (currentIndex + 1) % 10;
| Quote: | showSwimmer(currentIndex);
}
}
}
public void showSwimmer(int index)
{
nameDisplayField.setText
(swimmers[currentIndex].toString());
|
Here's another dangerous line.
You could check that swimmers[currentIndex] really
references an object before you try to use it, e.g.:
if (swimmers[currentIndex] != null)
{
nameDisplayField.setText
(swimmers[currentIndex].toString());
}
| Quote: | }
and in this case my swimmer array is now this :
Swimmer[] swimmers = new Swimmer[10];
swimmers[0] = new Swimmer("Jeremey");
|
Here's another possible source of error, and possibly just the one that's
causing this explicit error.
You have instantiated an array with room for 10 element, but how many
elements have you filled with actual references to objects? In the code you
sent you've only filled the first space and not the rest. If you don't fill
them the elements (swimmers[1], swimmers[2], etc) are only referencing
"null".
// Bjorn A
|
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Sat Apr 24, 2004 12:57 am Post subject: Re: Array Creation |
|
|
"Bjorn Abelli" wrote...
Just after I clicked on "send", I noticed another thing in you code. As
you're using "currentIndex" to get a specific Swimmer in the method
showSwimmer, there's not really any point in the argument "int index"...
| Quote: | public void showSwimmer(int index)
{
if (swimmers[currentIndex] != null)
{
nameDisplayField.setText
(swimmers[currentIndex].toString());
}
}
|
// Bjorn A
|
|
| Back to top |
|
 |
justin Guest
|
Posted: Sat Apr 24, 2004 2:59 am Post subject: Re: Array Creation |
|
|
Another thing
public showSwimmer(int index) {
txtField.setText(swimmers[currentIndex].toString());
}
}
this lines requires a return type..I tried putting void which is prob
dumb but cant think of anything else...and thats prob why the error
came up when i pressed on next pls help me somebody?
Christophe Vanfleteren <c.v4nfl3t3r3n (AT) pandora (DOT) be> wrote
| Quote: | justin wrote:
Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
To create your array of Swimmers:
Swimmer[] swimmers = new Swimmer[10];
to put an actual swimmer in the array:
swimmers[0] = new Swimmer("myname",...);
In your GUI class, add 2 buttons(previous and next) and a textfield
.
Also keep an index around, that will tell you which swimmer of the array you
are showing:
public class GUIClass ... {
private int currentIndex = 0;
private Swimmer[] swimmers;
public GUICLass() {
//create and fill the array of swimmers
...
//and show first swimmer
showSwimmer(0);
}
public showSwimmer(int index) {
txtField.setText(swimmers[currentIndex].toString());
}
}
Now you have to add listeners to your buttons to react to them being clicked
on:
nextButton.addActionListener(new ActionListener() {
public void actionPërformed(ActionEvent evt) {
currentIndex++;
showSwimmer(currentIndex);
}
});
ps. I (as in, I am), is spelled with a capital I in English.
|
|
|
| Back to top |
|
 |
Tony Morris Guest
|
Posted: Sat Apr 24, 2004 6:16 am Post subject: Re: Array Creation |
|
|
"justin" <jtwj (AT) hotmail (DOT) com> wrote
| Quote: | Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)
mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;
public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;
public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}
and i have another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys...would
appreciate it if included in the response what do i type in the GUI
class and where do i type some of the things and how do i display the
elements of the array onto a JTEextField on the GUI thanks
|
You should never directly instantiate a GregorianCalendar.
You should use the factory method provided by java.util.Calendar.
This code will not work in regions that do not use a Gregorian Calendar.
<pedant>This includes anywhere not on this planet (how is time measured if
your code is executing outside of the earth's gravitational pull
?)</pedant>, as well as some countries that use a different calendar system.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
|
|
| Back to top |
|
 |
justin Guest
|
Posted: Sat Apr 24, 2004 3:06 pm Post subject: Re: Array Creation |
|
|
So Bjorn what should I typing instead of public void showSwimmer(int
index)?i am really new sorry really sorry. thanks heaps appreaciate it
alot
"Bjorn Abelli" <DoNotSpam.bjorn_abelli (AT) hotmail (DOT) com> wrote
| Quote: | "Bjorn Abelli" wrote...
"justin" wrote...
Just after I clicked on "send", I noticed another thing in you code. As
you're using "currentIndex" to get a specific Swimmer in the method
showSwimmer, there's not really any point in the argument "int index"...
public void showSwimmer(int index)
{
if (swimmers[currentIndex] != null)
{
nameDisplayField.setText
(swimmers[currentIndex].toString());
}
}
// Bjorn A
|
|
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Sat Apr 24, 2004 3:16 pm Post subject: Re: Array Creation |
|
|
"justin" wrote...
| Quote: | So Bjorn what should I typing instead of public void
showSwimmer(int index)?i am really new sorry really
sorry. thanks heaps appreaciate it alot
|
In this case it's not necessary with any argument at all, e.g.:
public void showSwimmer()
{
if (swimmers[currentIndex] != null)
{
nameDisplayField.setText
(swimmers[currentIndex].toString());
}
}
// Bjorn A
|
|
| Back to top |
|
 |
justin Guest
|
Posted: Sun Apr 25, 2004 9:51 am Post subject: Re: Array Creation |
|
|
sorry but when i click next i still get this error whats wrong?
java.lang.NullPointerException
at myswimmerapplication.SwimmerGUI.showSwimmer(SwimmerGUI.java:237)
at myswimmerapplication.SwimmerGUI.actionPerformed(SwimmerGUI.java:230)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
|
|
| Back to top |
|
 |
Christophe Vanfleteren Guest
|
Posted: Sun Apr 25, 2004 9:57 am Post subject: Re: Array Creation |
|
|
justin wrote:
| Quote: | sorry but when i click next i still get this error whats wrong?
java.lang.NullPointerException
at myswimmerapplication.SwimmerGUI.showSwimmer(SwimmerGUI.java:237)
at myswimmerapplication.SwimmerGUI.actionPerformed(SwimmerGUI.java:230)
snip rest of stacktrace |
Bjorn explained to you how to interpret a stacktrace. I suggest you look at
his explanation again.
Anyway, without, at the very least, showing us what you have on line 237 in
SwimmerGUI.java, we will not be able to help you.
--
Kind regards,
Christophe Vanfleteren
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sun Apr 25, 2004 7:04 pm Post subject: Re: Array Creation |
|
|
On 25 Apr 2004 02:51:10 -0700, [email]jtwj (AT) hotmail (DOT) com[/email] (justin) wrote or
quoted :
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|