The program already can compile and run. But, when we enter the film's code, an error will occurred. We had no idea how to solve it :\

public class Entity {
	private String itemCode;
	private String title;
	private String description;
	private int language;
	private int time;
	private String productCompany;
	private int status;

	public Entity(){
		itemCode="E250";
		title="The Lookout";
		description="by Scott Frank";
		language=1;
		time=1900;
		productCompany="BB Sdn. Bhd.";
		status=1;
	}

    public Entity(String code, String title, String description, int language, int time, String company, int status) {
    	itemCode=code;
		this.title=title;
		this.description=description;
		this.language=language;
		this.time=time;
		productCompany=company;
		this.status=status;

    }

    public void setItemCode(String code){
    	itemCode=code;
    }

    public String getItemCode(){
    	return itemCode;
    }

    public void setTitle(String title){
    	this.title=title;
    }

    public String getTitle(){
    	return title;
    }

    public void setDescription(String description){
    	this.description=description;

    }

    public String getDescription(){
    	return description;
    }

    public void setLanguage(int language){
    	this.language=language;

    }

    public int getLanguage(){
    	return language;
    }

    public void setTime(int time){
    	this.time=time;

    }

    public int getTime(){
    	return time;
    }

    public void setProductCompany(String company){
    	productCompany=company;

    }

    public String getProductCompany(){
    	return productCompany;
    }

    public void setStatus(int status){
    	this.status=status;
    }

    public int getStatus(){
    	return status;
    }

    public static void CheckObject(Entity[] object,int a){
    	boolean test;
    	for(int i=0;i<a;i++){
    		if ((object[a].getItemCode()).equals(object[i].getItemCode())){

    			 test=true;
    			 System.out.println(test);}
    		else
    			test=false;

    	}

    }
import java.util.Scanner;

public class Collection {

    public static void main(String[] args) {

    	//add a film object to the arrays

    	Entity[] film=new Entity[100];
    	AddFilm(film);

    	for (int i=0; i<film.length; i++){
    		if (film[i]!=null){

    		System.out.println(film[i].getItemCode()+ film[i].getTitle() + film[i].getDescription()+ film[i].getLanguage()
    			+ film[i].getTime() + film[i].getProductCompany() +	film[i].getStatus() );
    		}
    	}
    }

    	public static void AddFilm(Entity[] film){
    		for(int i=0; i<film.length; i++){
    			if(film[i]==null){
    				Scanner scn=new Scanner(System.in);

    				System.out.print("Enter film's code :");
    				film[i].setItemCode(scn.next());

    				System.out.print("Enter film's title :");
    				film[i].setTitle(scn.next());

    				System.out.print("Enter film's description :");
    				film[i].setDescription(scn.next());

    				System.out.print("Select film's Language : \n1. English \n2. Malay \n3. Mandarin \n4. Indian \n5. Other \nChoose : ");
    				film[i].setLanguage(scn.nextInt());

    				System.out.print("Enter film's time (2400 hourse) :");
    				film[i].setTime(scn.nextInt());

    				System.out.print("Enter film production company name :");
    				film[i].setProductCompany(scn.next());

    				System.out.print("Select film's status :\n1. Pending \n2. Premiering \n3. Running \n4. Written-off \nSelection : ");
    				film[i].setStatus(scn.nextInt());
    			}
    		}
    	}

}

Recommended Answers

All 7 Replies

Entity[] film=new Entity[100]; This line of code creates an array of 100 Entity references, but they all have the initial value null.
So when you execute film[i].setItemCode(scn.next()); film is still null, and you get a null pointer exception (I assume that is the error you get. Next time please be specific about error messages etc).
You need to either
1. fill the array with new Entity's so you can later call their setX methods or
2. collect all the data for each Entity then use the constructor that sets all the values to create an Entity that you can put in the array.

you're trying to call a setter method on a NULL-value.
you have to instantiate your film object before you can use it.

just check your Addfilm method:

public static void AddFilm(Entity[] film){
    		for(int i=0; i<film.length; i++){
    			if(film[i]==null){
    				Scanner scn=new Scanner(System.in);

    				System.out.print("Enter film's code :");
    				film[i].setItemCode(scn.next());

    				System.out.print("Enter film's title :");
    				film[i].setTitle(scn.next());

    				System.out.print("Enter film's description :");
    				film[i].setDescription(scn.next());

    				System.out.print("Select film's Language : \n1. English \n2. Malay \n3. Mandarin \n4. Indian \n5. Other \nChoose : ");
    				film[i].setLanguage(scn.nextInt());

    				System.out.print("Enter film's time (2400 hourse) :");
    				film[i].setTime(scn.nextInt());

    				System.out.print("Enter film production company name :");
    				film[i].setProductCompany(scn.next());

    				System.out.print("Select film's status :\n1. Pending \n2. Premiering \n3. Running \n4. Written-off \nSelection : ");
    				film[i].setStatus(scn.nextInt());
    			}
    		}
}

there are two reasons you can check that the film object is, in fact not an instantiated object but a null value.
1. you call the method right after creating the array, without instantiating the elements
2. in your AddFilm method itself, you only run the code based on the check:

if ( film[i] == null )

what you should do there is:

public static void AddFilm(Entity[] film){
    		for(int i=0; i<film.length; i++){
    			if(film[i]==null){
                 // this is the constructor you will need to call
// public Entity(String code, String title, String description, int language, int time, //String company, int status)               
    				Scanner scn=new Scanner(System.in);

    				System.out.print("Enter film's code :");
    				String code = scn.next();

    				System.out.print("Enter film's title :");
    				String title = scn.next();

    				System.out.print("Enter film's description :");
    				String description = scn.next();

    				System.out.print("Select film's Language : \n1. English \n2. Malay \n3. Mandarin \n4. Indian \n5. Other \nChoose : ");
    				int language = scn.nextInt();

    				System.out.print("Enter film's time (2400 hourse) :");
    				int time = scn.nextInt();

    				System.out.print("Enter film production company name :");
    				String company = scn.next();

    				System.out.print("Select film's status :\n1. Pending \n2. Premiering \n3. Running \n4. Written-off \nSelection : ");
    				int status = scn.nextInt();

// and here you create the entity, and fill it in the right spot
film[i] = new Entity(code, title, description, language, time, company, status);
    			}
    		}
}

Wei Chien do you mind i take some idea from it? XD

Wei Chien do you mind i take some idea from it? XD

this is very basic Java coding, so if you think you're not allowed to use this kind of codes without anyone's permission, I'm afraid you'll spend more time asking permission than to actually write code :)

Haha, he's my clssmate dude. i am already tried with my code and also posted. Haha. XD

yep, I noticed. you also both made the same mistakes

The program already can compile and run. But, when we enter the film's code, an error will occurred. We had no idea how to solve it :\

public class Entity {
	private String itemCode;
	private String title;
	private String description;
	private int language;
	private int time;
	private String productCompany;
	private int status;

	public Entity(){
		itemCode="E250";
		title="The Lookout";
		description="by Scott Frank";
		language=1;
		time=1900;
		productCompany="BB Sdn. Bhd.";
		status=1;
	}

    public Entity(String code, String title, String description, int language, int time, String company, int status) {
    	itemCode=code;
		this.title=title;
		this.description=description;
		this.language=language;
		this.time=time;
		productCompany=company;
		this.status=status;

    }

    public void setItemCode(String code){
    	itemCode=code;
    }

    public String getItemCode(){
    	return itemCode;
    }

    public void setTitle(String title){
    	this.title=title;
    }

    public String getTitle(){
    	return title;
    }

    public void setDescription(String description){
    	this.description=description;

    }

    public String getDescription(){
    	return description;
    }

    public void setLanguage(int language){
    	this.language=language;

    }

    public int getLanguage(){
    	return language;
    }

    public void setTime(int time){
    	this.time=time;

    }

    public int getTime(){
    	return time;
    }

    public void setProductCompany(String company){
    	productCompany=company;

    }

    public String getProductCompany(){
    	return productCompany;
    }

    public void setStatus(int status){
    	this.status=status;
    }

    public int getStatus(){
    	return status;
    }

    public static void CheckObject(Entity[] object,int a){
    	boolean test;
    	for(int i=0;i<a;i++){
    		if ((object[a].getItemCode()).equals(object[i].getItemCode())){

    			 test=true;
    			 System.out.println(test);}
    		else
    			test=false;

    	}

    }
import java.util.Scanner;

public class Collection {

    public static void main(String[] args) {

    	//add a film object to the arrays

    	Entity[] film=new Entity[100];
    	AddFilm(film);

    	for (int i=0; i<film.length; i++){
    		if (film[i]!=null){

    		System.out.println(film[i].getItemCode()+ film[i].getTitle() + film[i].getDescription()+ film[i].getLanguage()
    			+ film[i].getTime() + film[i].getProductCompany() +	film[i].getStatus() );
    		}
    	}
    }

    	public static void AddFilm(Entity[] film){
    		for(int i=0; i<film.length; i++){
    			if(film[i]==null){
    				Scanner scn=new Scanner(System.in);

    				System.out.print("Enter film's code :");
    				film[i].setItemCode(scn.next());

    				System.out.print("Enter film's title :");
    				film[i].setTitle(scn.next());

    				System.out.print("Enter film's description :");
    				film[i].setDescription(scn.next());

    				System.out.print("Select film's Language : \n1. English \n2. Malay \n3. Mandarin \n4. Indian \n5. Other \nChoose : ");
    				film[i].setLanguage(scn.nextInt());

    				System.out.print("Enter film's time (2400 hourse) :");
    				film[i].setTime(scn.nextInt());

    				System.out.print("Enter film production company name :");
    				film[i].setProductCompany(scn.next());

    				System.out.print("Select film's status :\n1. Pending \n2. Premiering \n3. Running \n4. Written-off \nSelection : ");
    				film[i].setStatus(scn.nextInt());
    			}
    		}
    	}

}

//////////////////////////
I read your code ..I found the error is that you initialised array of objects ..but you not create object runtime you only create size of object class which you want but not create it runtime so...add this code after initialisation of array of object..


for(int i=0;i<array.length;i++){
class_object=new classname();
}
now it create run time your code must run with no error...

///////////////////////

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.