Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

very urgent help (2 hours left)

Thread view: 
Honyin Hau - 20 Apr 2006 21:30 GMT
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;
   }
Monique Y. Mudama - 20 Apr 2006 21:42 GMT
> Please help, have to hand in 2 hours time....
>
[quoted text clipped - 20 lines]
>         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?

Signature

monique

Help us help you:
http://www.catb.org/~esr/faqs/smart-questions.html

Honyin Hau - 20 Apr 2006 21:49 GMT
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

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

> monique

> Help us help you:

> http://www.catb.org/~esr/faqs/smart-questions.html
Hendrik Maryns - 21 Apr 2006 13:39 GMT
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,
>
[quoted text clipped - 5 lines]
>
> private ArrayList<Song> playlist;

Use List<Song> instead: do not code to implementations, code to an
interface.

> private int playListCount;
>
[quoted text clipped - 9 lines]
>
> playlist = new ArrayList<Song>();

But this stays the same!

> }
>
[quoted text clipped - 14 lines]
>
> Honyin

>>> Please help, have to hand in 2 hours time....
>
[quoted text clipped - 18 lines]
>
>>> for(Song s : list)

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.

>>> if(!playlist2.add(s)) // in case if add has error, it will not
>>> change playlist

Bad style.

>>> testing = false;
>
>>> if(testing==false) throw new NullPointerException("Error in:
>>> merge");

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

>>> for(Song s : list)
>
>>> playlist.add(s);

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

>>> return true;
>
>>> }

And please do read the useful link Monique gave you:

>> http://www.catb.org/~esr/faqs/smart-questions.html

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Patricia Shanahan - 20 Apr 2006 21:44 GMT
> Please help, have to hand in 2 hours time....
>
[quoted text clipped - 20 lines]
>         return true;
>     }

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

Patricia
Honyin Hau - 20 Apr 2006 21:50 GMT
Thank you, maybe I should try that now.

Honyin

>> Please help, have to hand in 2 hours time....
>>
[quoted text clipped - 25 lines]
>
> Patricia
Patricia Shanahan - 20 Apr 2006 22:08 GMT
> Thank you, maybe I should try that now.
>
> Honyin
...
>>For a quick fix, why not replace the foreach loop with an explicit
>>iteration through the elements of SongList?
>>
>>Patricia

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

Patricia
Honyin Hau - 20 Apr 2006 22:35 GMT
Seems like using the casting help but haven't test it yet.

Thanks to you and monique

>> Thank you, maybe I should try that now.
>>
[quoted text clipped - 9 lines]
>
> Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.