| | |
NullPointerexception ??
Thread Solved |
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[i].name=br.readLine();
System.out.println("Enter the key of the attribute");
attr[i].key=br.readLine();
System.out.println("Enter the datatype of the attribute");
attr[i].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[i].not_nul=true;
}
else
{
attr[i].not_nul=false;
}
System.out.println("Enter the default value of the attribute");
attr[i].default_value=br.readLine();
System.out.println("Enter the check for the attribute");
attr[i].check=br.readLine();
System.out.println("Enter the uniqueness(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr[i].unique=true;
}
else
{
attr[i].unique=false;
}
System.out.println("Enter the comment of the attribute");
attr[i].comment=br.readLine();
System.out.println("Enter the multivalue(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr[i].multivalue=true;
}
else
{
attr[i].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[i]=new ENTITY(ename,nofattr);
e[i].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[i].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
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[i].name=br.readLine();
System.out.println("Enter the key of the attribute");
attr[i].key=br.readLine();
System.out.println("Enter the datatype of the attribute");
attr[i].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[i].not_nul=true;
}
else
{
attr[i].not_nul=false;
}
System.out.println("Enter the default value of the attribute");
attr[i].default_value=br.readLine();
System.out.println("Enter the check for the attribute");
attr[i].check=br.readLine();
System.out.println("Enter the uniqueness(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr[i].unique=true;
}
else
{
attr[i].unique=false;
}
System.out.println("Enter the comment of the attribute");
attr[i].comment=br.readLine();
System.out.println("Enter the multivalue(y/n) of the attribute");
if(br.readLine().equals("Y") || br.readLine().equals("Y"))
{
attr[i].multivalue=true;
}
else
{
attr[i].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[i]=new ENTITY(ename,nofattr);
e[i].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[i].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
Batman is actually Bruce Wayne, Spider-Man is actually Peter Parker. He has to put on a costume to become Spider-Man. And it is in that characteristic Superman stands alone. Superman didn't become Superman. Superman was born Superman.Clark Kent is how Superman views us. And what are the characteristics of Clark Kent. He's weak... he's unsure of himself... he's a coward. Clark Kent is Superman's critique on the whole human race.
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).
Last edited by masijade; Jan 28th, 2008 at 3:57 am. Reason: typo
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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;
}
}
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;
}
}
Batman is actually Bruce Wayne, Spider-Man is actually Peter Parker. He has to put on a costume to become Spider-Man. And it is in that characteristic Superman stands alone. Superman didn't become Superman. Superman was born Superman.Clark Kent is how Superman views us. And what are the characteristics of Clark Kent. He's weak... he's unsure of himself... he's a coward. Clark Kent is Superman's critique on the whole human race.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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.
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
Batman is actually Bruce Wayne, Spider-Man is actually Peter Parker. He has to put on a costume to become Spider-Man. And it is in that characteristic Superman stands alone. Superman didn't become Superman. Superman was born Superman.Clark Kent is how Superman views us. And what are the characteristics of Clark Kent. He's weak... he's unsure of himself... he's a coward. Clark Kent is Superman's critique on the whole human race.
I have tried the above code using constructior but its still giving the same error plz suggest me where should i make the changes
Batman is actually Bruce Wayne, Spider-Man is actually Peter Parker. He has to put on a costume to become Spider-Man. And it is in that characteristic Superman stands alone. Superman didn't become Superman. Superman was born Superman.Clark Kent is how Superman views us. And what are the characteristics of Clark Kent. He's weak... he's unsure of himself... he's a coward. Clark Kent is Superman's critique on the whole human race.
Yes, you've added a constructor, but have you, anywhere in your code, called
Java Syntax (Toggle Plain Text)
attr[i] = new attribute();
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
•
•
Yes, you've added a constructor, but have you, anywhere in your code, called
Java Syntax (Toggle Plain Text)
attr[i] = new attribute();
Batman is actually Bruce Wayne, Spider-Man is actually Peter Parker. He has to put on a costume to become Spider-Man. And it is in that characteristic Superman stands alone. Superman didn't become Superman. Superman was born Superman.Clark Kent is how Superman views us. And what are the characteristics of Clark Kent. He's weak... he's unsure of himself... he's a coward. Clark Kent is Superman's critique on the whole human race.
![]() |
Similar Threads
- Word Association Game (Posting Games)
- [B]NullPointerException[/B] (Java)
- Multi class multi form j2me app getting NullPointerException (Java)
- NullPointerException: (Java)
- Thinking OOP!: From BASIC to JAVA (Java)
- Need some help with a Java lab... (Java)
Other Threads in the Java Forum
- Previous Thread: very basic
- Next Thread: Accessible Gui text
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






