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