Results 1 to 9 of 9
  1. #1
    tvenhc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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()); 
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Your parameters do not match.
    Java Code:
    new Film();
    Film(String title, boolean fWide, boolean fFull,boolean fSpecial,String fThumbnail)

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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).

  5. #5
    tvenhc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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).
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    Film[] film = new Film[10];
    for(int i = 0; i < 10; i++)
    {
        film[i] = new Film();
    }
    If you are saying the above code was provided to you then I find that hard to believe.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by tvenhc View Post
    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.
    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).

  8. #8
    tvenhc is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    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.
    So what did you expect, spoonfeeding?

    db

Similar Threads

  1. Need Help with NullPointerException Error
    By waterisgood5 in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 07:27 PM
  2. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 02:31 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. awt TextField nullPointerException error
    By k2k in forum AWT / Swing
    Replies: 3
    Last Post: 02-24-2009, 04:24 AM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •