NullPointerexception ??

Thread Solved

Join Date: Aug 2007
Posts: 34
Reputation: arkaprava is an unknown quantity at this point 
Solved Threads: 0
arkaprava's Avatar
arkaprava arkaprava is offline Offline
Light Poster

NullPointerexception ??

 
0
  #1
Jan 28th, 2008
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
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: NullPointerexception ??

 
0
  #2
Jan 28th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 34
Reputation: arkaprava is an unknown quantity at this point 
Solved Threads: 0
arkaprava's Avatar
arkaprava arkaprava is offline Offline
Light Poster

Re: NullPointerexception ??

 
0
  #3
Jan 28th, 2008
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;


}
}
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: NullPointerexception ??

 
0
  #4
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 34
Reputation: arkaprava is an unknown quantity at this point 
Solved Threads: 0
arkaprava's Avatar
arkaprava arkaprava is offline Offline
Light Poster

Re: NullPointerexception ??

 
0
  #5
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 34
Reputation: arkaprava is an unknown quantity at this point 
Solved Threads: 0
arkaprava's Avatar
arkaprava arkaprava is offline Offline
Light Poster

Re: NullPointerexception ??

 
0
  #6
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: NullPointerexception ??

 
0
  #7
Jan 28th, 2008
Well you still havent initialized the array members from what i can see.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: NullPointerexception ??

 
0
  #8
Jan 28th, 2008
Yes, you've added a constructor, but have you, anywhere in your code, called
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 34
Reputation: arkaprava is an unknown quantity at this point 
Solved Threads: 0
arkaprava's Avatar
arkaprava arkaprava is offline Offline
Light Poster

Re: NullPointerexception ??

 
0
  #9
Jan 29th, 2008
Originally Posted by masijade View Post
Yes, you've added a constructor, but have you, anywhere in your code, called
  1. attr[i] = new attribute();
Thanx Problem is solved
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC