Results 1 to 9 of 9
- 03-31-2011, 01:46 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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.
Java 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()); } }
- 03-31-2011, 02:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are trying to fill the list with default film objects. However, there is not default film constructor, you would have to create each film object usig the constructor that takes arguments.
- 03-31-2011, 02:11 AM #3
Your parameters do not match.
Java Code:new Film(); Film(String title, boolean fWide, boolean fFull,boolean fSpecial,String fThumbnail)
- 03-31-2011, 02:14 AM #4
I do have to wonder why you create an array of Film objects (correctly) in the createList method and then back in the main method you try to create another array (incorrectly).
- 03-31-2011, 03:29 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
This file (Film.java) was already half filled when I got it from the instructor. The only thing I had to add to this file is set and get methods for the instance variables. My intent is actually to pass down the array to JList in different file, but if I can make it work in main() method to output the list, then I can do it on JList. So, any suggestions what I could do? And before suggesting changing the createList method to static or accept variables to pass down won't work because it does not meet the instructor's set of rules to this assignment.
- 03-31-2011, 03:36 AM #6
If you are saying the above code was provided to you then I find that hard to believe.Java Code:Film[] film = new Film[10]; for(int i = 0; i < 10; i++) { film[i] = new Film(); }
- 03-31-2011, 03:39 AM #7
Why would I suggest changing code that is correct? Changing code that is incorrect is a much better approach. The incorrect code has been pointed out above.
What I would suggest is to add another method to iterate over the array of film objects and dsiplay them. Then you can call this method from the main method (just like createList).
- 03-31-2011, 05:16 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Haha.. Maybe I should've been more clear about what I meant but please don't give a nuance such that I'm a terrible person. What I meant was the instructor wrote the class Film and class FilmList, but I made the main() method inside the class FilmList. Since I'm a beginner, the replies didn't really much helped me because they just pointed me out what's wrong with the code. But I know where the code is wrong, and that is why I tried to explain how I could fix that problem. Thank you anyways Junky.
- 03-31-2011, 06:09 AM #9
Similar Threads
-
Need Help with NullPointerException Error
By waterisgood5 in forum New To JavaReplies: 2Last Post: 11-10-2010, 07:27 PM -
NullPointerException error
By Aggror in forum New To JavaReplies: 2Last Post: 09-29-2010, 02:31 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
awt TextField nullPointerException error
By k2k in forum AWT / SwingReplies: 3Last Post: 02-24-2009, 04:24 AM -
ERROR: nullPointerException
By mathias in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks