PDA

View Full Version : Java Error: array required, but PRmList found


brooksn
Mar 11, 2003, 12:20 AM
Here is my code: I get the error:
Array required, but PRmList found. Any help would be GREATLY appreciated. Thanks!

Public class PRm
{
Private String PRm;
Private String movieTitle;
Private String movieRating;
Private int seats;

Public PRm(String prm, String movie, String rating, int sts)
{
PRm=prm;
MovieTitle=movie;
MovieRating=rating;
Seats=sts;
}
Public String getmovieTitle()
{
Return movieTitle;
}
}

Public class PRmList
{
PRm[] pRmList;

Public void createList()
{
PRmList = new PRm[5];
PRmList[0]= new PRm("PR1", "Fast and the Furious", "R", 90);
PRmList[1]= new PRm("PR2", "Usual Suspects", "R", 70);
PRmList[2]= new PRm("PR3", "Spy Game", "R", 80);
PRmList[3]= new PRm("PR4", "Die Hard 3", "R", 60);
PRmList[4]= new PRm("PR5", "Lord of the Rings", "R", 100);
}
}

Import java.io.*;
Public class Test
{
Public static void main(String[] args)
{
PRmList lis = new PRmList();
Lis.createList();

Lis[0].getmovieTitle(); // Error is here
}
}

YDG
Mar 17, 2003, 04:07 AM
Hi
The problem is in the way you were referencing the getMovieTitle() method in your main method. The variable "lis" is a PRmlist object, but "getMovieTitle" is not a method of that class, it's a method of the PRm class. Even though the PRmlist object contains an array of PRm objects, you can't access the PRm classes methods directly in this way. If you had made the PRmlist extend the PRm class (called 'inheritance') when you designed the class, then yes it would've worked in the way you were thinking (with a slight modification to what you were doing). What you need to do is access the variable in the PRmlist object you created (pRmList = new PRm[5]), and get the properties of the object (PRm) that the variable is storing. So your code should look like this:

Public class Test
{
Public static void main(String[] args)
{
PRmList lis = new PRmList();
Lis.createList();
//lis[0].getmovieTitle(); // Error is here

Lis.pRmList[0].getmovieTitle() //<--- this is the correct way

//System.out.println(lis.pRmList[0].getmovieTitle()); //<use this to see the result;)
}
}

One other thing you were doing wrong, was trying to treat 'lis' as an array, but in your declaration it was not specified as one (instead of "PRmList lis = new PRmList();", you would need to declare it as "PRmList lis[] = new PRmList();" or "PRmList[] lis = new PRmList();"). I'm guessing you were trying to use an array so you could access the array you created in the PRmlist class, which is on the right track, but an array already exists in the PRmlist class, all you need to do is directly access that array with this:
Lis.pRmList[x] (where x is a number)
Lis is an instance of the PRmlist class, you can directly access any of the PRmlist variables and methods.

Hope that's slightly understandable ;)

brooksn
Mar 17, 2003, 04:07 PM
Thank you so much, very understandable. :)