Please help on nullpointerexception error.. Cannot initallize the array properly.
Hello, I am currently a student who is taking Intro to Java programming.
While I was doing my assignment, I got stuck in a particular area. Please help me to get through this.
So, what I am trying to do is that from an object array of 10 slots and 5 indivisual fields (Eg. Each array slot will have Title, boolean Wide, boolean Full, etc.), I would like to get a list of the Titles only as an output.
However, when I try to compile and run the codings, I get java.lang.NullPointerException.. And I found that I haven't initiallized the array correctly, so I added:
------------------------------------------------------------------------
for(int i = 0; i < 10; i++)
{
film[i] = new Film();
}
------------------------------------------------------------------------
But then I get
Film.java:102: cannot find symbol
symbol : constructor Film()
location: class Film
film[i] = new Film();
And since this is an assignment for my class, I cannot change class Film (exception of set/get methods) and class FilmList. Those are the restrictions I have. I am attaching my codes below for better understanding of my situation. Please help me to get through this. And thank you in advance!:D
Regards,
Steven.
Code:
class Film
{
private String filmTitle;
private boolean filmFormatWide;
private boolean filmFormatFull;
private boolean filmSpecial;
private String filmThumbnail;
Film(String title, boolean fWide, boolean fFull,boolean fSpecial,String fThumbnail)
{
filmTitle = title;
filmFormatWide = fWide;
filmFormatFull = fFull;
filmSpecial = fSpecial;
filmThumbnail = fThumbnail;
}
public void setTitle(String title)
{
filmTitle = title;
}
public void setFormatWide(boolean fWide)
{
filmFormatWide = fWide;
}
public void setFormatFull(boolean fFull)
{
filmFormatFull = fFull;
}
public void setSpecial(boolean fSpecial)
{
filmSpecial = fSpecial;
}
public void setThumbnail(String fThumbnail)
{
filmThumbnail = fThumbnail;
}
public String getTitle() ////////////////////////////////////////////////
{
return filmTitle;
}
public boolean getFormatWide()
{
return filmFormatWide;
}
public boolean getFormatFull()
{
return filmFormatFull;
}
public boolean getSpecial()
{
return filmSpecial;
}
public String getThumbnail()
{
return filmThumbnail;
}
}
class FilmList
{
Film[] fList;
public void createList()
{
fList = new Film[10];
fList[0] = new Film("Austin Powers in Goldmember",true,true,false,"apowers3.jpg");
fList[1] = new Film("Enemy At the Gates",true,false,true,"enemyatthegates.jpg");
fList[2] = new Film("Fist of Fury",false,true,true,"fistoffury.jpg");
fList[3] = new Film("Hearts in Atlantis",true,false,false,"heartsinatlantis.jpg");
fList[4] = new Film("Judge Dredd",false,true,false,"judgedredd.jpg");
fList[5] = new Film("Star Trek Nemesis",true,true,false,"startreknemesis.jpg");
fList[6] = new Film("Toy Story 2",true,true,false,"toystory2.jpg");
fList[7] = new Film("Training Day",true,false,true,"trainingday.jpg");
fList[8] = new Film("Twister",false,true,true,"twister.jpg");
fList[9] = new Film("X Files",true,false,false,"xfiles.jpg");
}
public static void main(String[] args)
{
FilmList flmlist = new FilmList();
flmlist.createList();
Film[] film = new Film[10];
for(int i = 0; i < 10; i++)
{
film[i] = new Film();
}
System.out.println(film[0].getTitle());
}
}