very urgent help (2 hours left)

H

Honyin Hau

Please help, have to hand in 2 hours time....

I don't understand what the error message means and how to change it

Thanks million in advance


Error message selecting the first for(Song s : list): foreach not applicable
to expression type

Code:
public boolean merge(SongList list) throws NullPointerException
{
ArrayList<Song> playlist2 = playlist;
boolean testing = true;
for(Song s : list)
if(!playlist2.add(s)) // in case if add has error, it will not
change playlist
testing = false;
if(testing==false) throw new NullPointerException("Error in:
merge");
for(Song s : list)
playlist.add(s);
return true;
}
 
M

Monique Y. Mudama

Please help, have to hand in 2 hours time....

I don't understand what the error message means and how to change it

Thanks million in advance


Error message selecting the first for(Song s : list): foreach not applicable
to expression type

Code:
public boolean merge(SongList list) throws NullPointerException
{
ArrayList<Song> playlist2 = playlist;
boolean testing = true;
for(Song s : list)
if(!playlist2.add(s)) // in case if add has error, it will not
change playlist
testing = false;
if(testing==false) throw new NullPointerException("Error in:
merge");
for(Song s : list)
playlist.add(s);
return true;
}

The problem is in the code you didn't post:

What's a SongList? Do you think it's in some way related to playlist
or playlist2?
 
P

Patricia Shanahan

Honyin said:
Please help, have to hand in 2 hours time....

I don't understand what the error message means and how to change it

Thanks million in advance


Error message selecting the first for(Song s : list): foreach not applicable
to expression type

Code:
public boolean merge(SongList list) throws NullPointerException
{
ArrayList<Song> playlist2 = playlist;
boolean testing = true;
for(Song s : list)
if(!playlist2.add(s)) // in case if add has error, it will not
change playlist
testing = false;
if(testing==false) throw new NullPointerException("Error in:
merge");
for(Song s : list)
playlist.add(s);
return true;
}

For a quick fix, why not replace the foreach loop with an explicit
iteration through the elements of SongList?

Patricia
 
H

Honyin Hau

Thanks for helping,

This is the beginning of the code for PlayList:

public class PlayList implements SongList

{

private ArrayList<Song> playlist;

private int playListCount;


/**

* Constructor

*/

public PlayList(){

playListCount = 0;

playlist = new ArrayList<Song>();

}

.....

}

This is the beginning of SongList

public interface SongList{}

This is the beginning of Song object:

public class Song implements Comparable<Song>{}

I think I haven't quite catch the meaning of interface, implememnt and
foreach

Is this enough for you to help? If not I will post all of it

million thanks

Honyin





On 2006-04-20, Honyin Hau penned:

The problem is in the code you didn't post:

What's a SongList? Do you think it's in some way related to playlist
 
H

Honyin Hau

Thank you, maybe I should try that now.

Honyin

Patricia Shanahan said:
For a quick fix, why not replace the foreach loop with an explicit
iteration through the elements of SongList?

Patricia
 
P

Patricia Shanahan

Honyin said:
Thank you, maybe I should try that now.

Honyin ....

You may still have problems, but with the explicit iteration you may
find them easier to fix.

Patricia
 
H

Honyin Hau

Seems like using the casting help but haven't test it yet.

Thanks to you and monique
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Please do not top-post. See my comments interspersed with your post.
And you might want to check for your newsreader inserting unwanted newlines.

Honyin Hau schreef:
Thanks for helping,

This is the beginning of the code for PlayList:

public class PlayList implements SongList

{

private ArrayList<Song> playlist;

private int playListCount;


/**

* Constructor

*/

public PlayList(){

playListCount = 0;

playlist = new ArrayList<Song>();

But this stays the same!
}

....

}

This is the beginning of Song object:

public class Song implements Comparable<Song>{}

I think I haven't quite catch the meaning of interface, implememnt and
foreach

Is this enough for you to help? If not I will post all of it

million thanks

Honyin

You can only use the foreach syntax on objects that implement the
Iterable interface. SongList does not:
This is the beginning of SongList

public interface SongList{}

So either do

public interface SongList extends Iterable { ... }

or do your iteration manually.

Bad style.

Huh? Why a NullPointerException? I don’t see any null pointer
anywhere. Do not abuse RuntimeExceptions.

You could make you code more legible by inserting {} around the commands
after if and for statements.

And please do read the useful link Monique gave you:

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFESNKZe+7xMGD3itQRAvL+AJ9L92/5lHvp714yuHmTX+z7mLjsAACfSR/3
Cfmo7L9eGFaJ6xt9Eo6dsMQ=
=on/a
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top