| | |
ArrayIndexOutofBoundException
Thread Solved |
Since Input of ENTITY and of it's attribute is ok
I am trying to op the value of the entity and it's attribute details
so my modified code abt ENTITY is:
package datamodel;
import java.io.*;
import java.util.*;
public class ENTITY{
String entity_name;
int no_of_attribute;
int no_of_entity;
attribute attr1;
public ENTITY(String ename,int nofattr,int nofentity)throws IOException
{
entity_name=ename;
no_of_attribute=nofattr;
no_of_entity=nofentity;
}
attribute[] attr=new attribute[no_of_attribute];
public void setAttributeValues()throws IOException,java.lang.ArrayIndexOutOfBoundsException
{
System.out.println("We are prompting for entering data....");
for(int i=0;i<no_of_attribute;i++)
{
attr[i]=new attribute();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the attribute=");
attr[i].name=br.readLine();
System.out.print("Enter the key of the attribute=");
attr[i].key=br.readLine();
System.out.print("Enter the datatype of the attribute=");
attr[i].datatype= br.readLine();
System.out.print("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.print("Enter the default value of the attribute=");
attr[i].default_value=br.readLine();
System.out.print("Enter the check for the attribute=");
attr[i].check=br.readLine();
System.out.print("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.print("Enter the comment of the attribute=");
attr[i].comment=br.readLine();
System.out.print("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;
}
}
}
public void getAttributeValues(){
for(int i=0;i<no_of_entity;i++){
System.out.println("Name Of Entity="+this.entity_name);
for(int j=0;j<no_of_attribute;j++){
System.out.println("The name of the attribute="+this.attr[i].name);
System.out.println("The key of the attribute="+this.attr[i].key);
System.out.println("The datatype of the attribute="+this.attr[i].datatype);
System.out.println("The not_null property (y/n) of the attribute="+this.attr[i].not_nul);
System.out.println("The default value of the attribute="+this.attr[i].default_value);
System.out.println("The check for the attribute="+this.attr[i].check);
System.out.println("The uniqueness(y/n) of the attribute="+this.attr[i].unique);
System.out.println("The comment of the attribute="+this.attr[i].comment);
System.out.println("The multivalue(y/n) of the attribute="+this.attr[i].multivalue);
}
}
}
}
where bold is function which op the value stored into the entity
to call the function I have written another change in the erd.java (where the main class remains)code:
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,nofentity);
e[i].setAttributeValues();
e[i].getAttributeValues();
}
}
}
My problem is when I put the line: attribute[] attr=new attribute[no_of_attribute]; inside of the function public void setAttributeValues() in the class ENTITY a lot of compilation error is detected because getArrtibuteValues() does not get the array [ because the array is local member of the fn setAttributeValues() ] but after omitting the the function setAttributeValues() all input are inputted smoothly. When I put the line attribute[] attr=new attribute[no_of_attribute]; outside any function(as shown in this code) then ArrayIndexOutOfBoundException shown in line : attr[i].name=br.readLine(); when I input the values of the arribute of the entity. throwing of the exception doesnot result any fruitful
plz suggest me something what modification I should do in our code of ENTITY class to avoid such exception
I am trying to op the value of the entity and it's attribute details
so my modified code abt ENTITY is:
package datamodel;
import java.io.*;
import java.util.*;
public class ENTITY{
String entity_name;
int no_of_attribute;
int no_of_entity;
attribute attr1;
public ENTITY(String ename,int nofattr,int nofentity)throws IOException
{
entity_name=ename;
no_of_attribute=nofattr;
no_of_entity=nofentity;
}
attribute[] attr=new attribute[no_of_attribute];
public void setAttributeValues()throws IOException,java.lang.ArrayIndexOutOfBoundsException
{
System.out.println("We are prompting for entering data....");
for(int i=0;i<no_of_attribute;i++)
{
attr[i]=new attribute();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the attribute=");
attr[i].name=br.readLine();
System.out.print("Enter the key of the attribute=");
attr[i].key=br.readLine();
System.out.print("Enter the datatype of the attribute=");
attr[i].datatype= br.readLine();
System.out.print("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.print("Enter the default value of the attribute=");
attr[i].default_value=br.readLine();
System.out.print("Enter the check for the attribute=");
attr[i].check=br.readLine();
System.out.print("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.print("Enter the comment of the attribute=");
attr[i].comment=br.readLine();
System.out.print("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;
}
}
}
public void getAttributeValues(){
for(int i=0;i<no_of_entity;i++){
System.out.println("Name Of Entity="+this.entity_name);
for(int j=0;j<no_of_attribute;j++){
System.out.println("The name of the attribute="+this.attr[i].name);
System.out.println("The key of the attribute="+this.attr[i].key);
System.out.println("The datatype of the attribute="+this.attr[i].datatype);
System.out.println("The not_null property (y/n) of the attribute="+this.attr[i].not_nul);
System.out.println("The default value of the attribute="+this.attr[i].default_value);
System.out.println("The check for the attribute="+this.attr[i].check);
System.out.println("The uniqueness(y/n) of the attribute="+this.attr[i].unique);
System.out.println("The comment of the attribute="+this.attr[i].comment);
System.out.println("The multivalue(y/n) of the attribute="+this.attr[i].multivalue);
}
}
}
}
where bold is function which op the value stored into the entity
to call the function I have written another change in the erd.java (where the main class remains)code:
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,nofentity);
e[i].setAttributeValues();
e[i].getAttributeValues();
}
}
}
My problem is when I put the line: attribute[] attr=new attribute[no_of_attribute]; inside of the function public void setAttributeValues() in the class ENTITY a lot of compilation error is detected because getArrtibuteValues() does not get the array [ because the array is local member of the fn setAttributeValues() ] but after omitting the the function setAttributeValues() all input are inputted smoothly. When I put the line attribute[] attr=new attribute[no_of_attribute]; outside any function(as shown in this code) then ArrayIndexOutOfBoundException shown in line : attr[i].name=br.readLine(); when I input the values of the arribute of the entity. throwing of the exception doesnot result any fruitful
plz suggest me something what modification I should do in our code of ENTITY class to avoid such exception
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: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
Oh gosh, that's a large post! First, I don't think you require throws IOException in the constructor of ENTITY. Second, please use the Java convention for identifier names, it's your friend honest
Instead of ENTITY it should be Entity.
At first glance, I think your error is because of:
Oh and it should be Attribute and not attribute as per the Java naming convention
One last thing, never iterate over an array like this:
If you change it to:
At first glance, I think your error is because of:
attribute[] attr=new attribute[no_of_attribute]; though I'm actually a bit in conflict about that. So try puttingattr = new attribute[no_of_attribute]; within your constructor. And simply haveattribute attr; declared as a field variable. I'm not sure how good your Java is but if you don't understand what I've said, please let me know. Moveover, I'm not sure if this will resolve your problem but it's easier to test that out than for me to look at all of your code. Oh and please use the DaniWeb tags when quoting programming code Oh and it should be Attribute and not attribute as per the Java naming convention
One last thing, never iterate over an array like this:
for(int i=0;j<no_of_attribute;j++)If you change it to:
for(int i=0;j<attr.length;j++) then you will stop getting that Exception Last edited by PoovenM; Jan 30th, 2008 at 3:17 am. Reason: After thought
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
Java Syntax (Toggle Plain Text)
attribute[] attr=new attribute[no_of_attribute];
Java Syntax (Toggle Plain Text)
for(int i=0;i<no_of_entity;i++){
Java Syntax (Toggle Plain Text)
for(int j=0;j<no_of_attribute;j++){
j is never used
Java Syntax (Toggle Plain Text)
System.out.println("The name of the attribute="+this.attr[i].name);
but i might be bigger than the size of attr, which is no_of_attribute
because no_of_entity might be bigger than no_of_attribute
--
Index of mp3
Index of mp3
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
hehe Indeed he does have his indices mixed up! But I would like to check up on the instantiation of the array outside the constructor. I think that the new value of no_of_attributes would be updated before the array is created, but I'm not sure. I'll check it out later. Hope that solves his problem though.
•
•
•
•
Oh gosh, that's a large post! First, I don't think you require throws IOException in the constructor of ENTITY. Second, please use the Java convention for identifier names, it's your friend honestInstead of ENTITY it should be Entity.
At first glance, I think your error is because of:attribute[] attr=new attribute[no_of_attribute];though I'm actually a bit in conflict about that. So try puttingattr = new attribute[no_of_attribute];within your constructor. And simply haveattribute attr;declared as a field variable. I'm not sure how good your Java is but if you don't understand what I've said, please let me know. Moveover, I'm not sure if this will resolve your problem but it's easier to test that out than for me to look at all of your code. Oh and please use the DaniWeb tags when quoting programming code![]()
Oh and it should be Attribute and not attribute as per the Java naming convention
One last thing, never iterate over an array like this:for(int i=0;j<no_of_attribute;j++)
If you change it to:for(int i=0;j<attr.length;j++)then you will stop getting that Exception
and initializing it into the constructor solved the problem
PS Sorry for late reply
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
- question (Java)
Other Threads in the Java Forum
- Previous Thread: Help me in Sun Certified Java Associate (SCJA) exam
- Next Thread: Turing Machine Code for Java
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project radio recursion reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows





