For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > Array Creation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Array Creation
justin

2004-04-23, 9:33 am

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,best
Time;
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
Christophe Vanfleteren

2004-04-23, 9:33 am

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,best
Time;
> 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
justin

2004-04-23, 8:43 pm

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,8);

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@pandora.be> wrote in message news:<dq8ic.83470$%l7.5377160@phobos.telenet-ops.be>...
> justin wrote:
>
>
>
> 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.

justin

2004-04-23, 8:43 pm

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:\JBuilderX\jdk1.4\bin\javaw -classpath "C:\Documents and
Settings\Justin\jbproject\MySwimmerAppli
cation\classes;C:\JBuilderX\jdk1.4\demo\jfc\Java2D\Java2Demo.jar;C:\JBuilderX\jdk1.4\demo\plugin\jfc\Java2D\Java2Demo.jar;C:\JBuilderX\jdk1.4\jre\javaws\javaws.jar;C:\JBuilderX\jdk1.4\jre\lib\charsets.jar;C:\JBuilde
rX\jdk1.4\jre\lib\ext\dnsns.jar;C:\JBuilderX\jdk1.4\jre\lib\ext\ldapsec.jar;C:\JBuilderX\jdk1.4\jre\lib\ext\localedata.jar;C:\JBuilderX\jdk1.4\jr
\lib\ext\sunjce_provider.jar;C:\JBuilderX\jdk1.4\jre\lib\im\indicim.jar;C:\JBuilderX\jdk1.4\jre\lib\im\thaiim.jar;C:\JBuilderX\jdk1.4\jre\lib\jce.jar;C:\JBuilderX\jdk1.4\jre\lib\jsse.jar;C:\JBuilderX\jdk1.4\jre\lib\plugin.jar;C:\JBuilderX\jdk1.4\jre\lib\r
t.jar;C:\JBuilderX\jdk1.4\jre\lib\sunrsasign.jar;C:\JBuilderX\jdk1.4\lib\dt.jar;C:\JBuilderX\jdk1.4\lib\htmlconverter.jar;C:\JBuilderX\jdk1.4\lib
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(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread. pumpEventsForHierarchy(EventDispatchThre
ad.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@pandora.be> wrote in message news:<dq8ic.83470$%l7.5377160@phobos.telenet-ops.be>...
> justin wrote:
>
>
>
> 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.

Bjorn Abelli

2004-04-23, 9:37 pm

"justin" wrote...

The error you're getting tells you what's wrong:

> 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:

> at myswimmerapplication.SwimmerGUI.showSwimmer
> (SwimmerGUI.java:220)


....the method showSwimmer is in turn called at row 214, etc...

> at myswimmerapplication.SwimmerGUI.actionPerformed
> (SwimmerGUI.java:214)


[snip]

> 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;

> 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());
}
> }
>
>
> 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


Bjorn Abelli

2004-04-23, 9:37 pm


"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


justin

2004-04-23, 11:33 pm

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@pandora.be> wrote in message news:<dq8ic.83470$%l7.5377160@phobos.telenet-ops.be>...
> justin wrote:
>
>
>
> 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.

Tony Morris

2004-04-24, 3:42 am

"justin" <jtwj@hotmail.com> wrote in message
news:61a979f4.0404230419.303d7907@posting.google.com...
> 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,best
Time;
> 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)



justin

2004-04-24, 1:32 pm

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@hotmail.com> wrote in message news:<c6ce12$agprv$1@ID-73292.news.uni-berlin.de>...
> "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"...
>
>
> // Bjorn A

Bjorn Abelli

2004-04-24, 1:32 pm


"justin" wrote...

> 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


justin

2004-04-25, 6:34 am

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(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread. pumpEventsForHierarchy(EventDispatchThre
ad.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)
Christophe Vanfleteren

2004-04-25, 6:34 am

justin wrote:

> 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
Roedy Green

2004-04-25, 4:38 pm

On 25 Apr 2004 02:51:10 -0700, jtwj@hotmail.com (justin) wrote or
quoted :

>sorry but when i click next i still get this error whats wrong?
>java.lang.NullPointerException

see http://mindprod.com/jgloss/errormessages.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com