I m going to build a data modelling software project by java like DDS LITE
I am coding the algorithmic part of the project where I have to build some composite data at the preliminary phase of the implementation of the algorithms, such composite data items are entity , relationship & attribute , so far I have done entity and the attribute data items

The corrosponding code of Entity class is:

package datamodel;


import java.io.*;
import java.util.*;


public class ENTITY{


String name;
int noa;


public ENTITY()
{
}
public ENTITY(String ename,int nofattr) throws IOException,ArrayIndexOutOfBoundsException
{
name=ename;
noa=nofattr;



}


public void attr_name_initialization()throws IOException,ArrayIndexOutOfBoundsException
{
attribute[] attr=new attribute[noa];
System.out.println("We are prompting for entering data");


for(int i=0;i<noa;i++)
{


BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the attribute");
attr.name=br.readLine();


System.out.println("Enter the key of the attribute");
attr.key=br.readLine();


System.out.println("Enter the datatype of the attribute");
attr.datatype=  br.readLine();


System.out.println("Enter the not_null property (y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("y"))
{
attr.not_nul=true;
}
else
{
attr.not_nul=false;
}


System.out.println("Enter the default value of the attribute");
attr.default_value=br.readLine();


System.out.println("Enter the check for the attribute");
attr.check=br.readLine();


System.out.println("Enter the uniqueness(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr.unique=true;
}
else
{
attr.unique=false;


}


System.out.println("Enter the comment of the attribute");
attr.comment=br.readLine();


System.out.println("Enter the multivalue(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr.multivalue=true;
}
else
{
attr.multivalue=false;


}


}


}
}

In the above code name denotes the name of the entity and variable noa denotes no of attributes it may have .
To implement the attribute class I have coded the attribute class:

package datamodel;
import java.io.*;
import java.util.*;



abstract class attribute
{
public String name;
public String key;
public String datatype;
public Boolean not_nul;
public String default_value;
public String check;
public Boolean unique;
public String comment;
public Boolean multivalue;
}

where the variables in the attribute are the property of an attribute.
Since our project is in starting phase we are providing data through console checking whether the program is running ok. So we also had written another class erd:

package datamodel;
import java.io.*;
import java.util.*;


public class erd {


public static void main(String args[])throws IOException{
String ename;
int nofentity;
int nofattr;
System.out.print("Enter no. of entities=");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
nofentity=Integer.parseInt(br.readLine());
ENTITY[] e=new ENTITY[nofentity];



for(int i=0;i<nofentity;i++)
{
System.out.print("Enter name of entity=");
ename=br.readLine();
System.out.print("Enter no. of attributes=");
nofattr=Integer.parseInt(br.readLine());
e=new ENTITY(ename,nofattr);
e.attr_name_initialization();


}


}



}

The compilation of the classes are ok. but when I am trying to run the project 1stly it takes value from the console den a NullPointerException was happened at the following line of the entity class

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the attribute");attr.name=br.readLine();

Then I ve copied and pasted the codepart of the attr_name_initialization into the constructor part of the ENTITY class but the same NullPointerException (in the same line)is happening

throwing of NullPointerException did not result any fruitful
Plz suggest me what modification is necessary in the code for proper running of the code

You've declarred an array (attr) of type attrribute, but the elements of that array, until defined, are null. So, when you attempt to dereference one of those elements (that are currently null) by referencing the "name" attribute you get an NPE. You need to define each element in the array with new attribute() (or whatever constructor you use).

I have not tried but asking you whether a constructor like this can be used :

public class attribute{
public String name;
public String key;
public String datatype;
public Boolean not_nul;
public String default_value;
public String check;
public Boolean unique;
public String comment;
public Boolean multivalue;

public attribute()
{
this.name="";
this.key="";
this.datatype="";
this.not_nul=false;
this.default_value="";
this.check="";
this.unique=false;
this.comment="";
this.multivalue=false;


}
}

Yeh thats called a default constructor. You don't really need the this keyword in front of the member names.

A word of advice class members should be private or protected and never unless theres good reason for it to be public. Setter and Getting functions should be used to set the private data members of your class.

I have tried the above code snippet but the same error has occured.Plz suggest me where should i make the changes

I have tried the above code using constructior but its still giving the same error plz suggest me where should i make the changes

Well you still havent initialized the array members from what i can see.

Yes, you've added a constructor, but have you, anywhere in your code, called

attr[i] = new attribute();

Yes, you've added a constructor, but have you, anywhere in your code, called

attr[i] = new attribute();

Thanx Problem is solved

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.