943,589 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4959
  • Java RSS
Jan 30th, 2008
0

ArrayIndexOutofBoundException

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
arkaprava is offline Offline
34 posts
since Aug 2007
Jan 30th, 2008
0

Re: ArrayIndexOutofBoundException

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: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
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 30th, 2008
0

Re: ArrayIndexOutofBoundException

I just noticed, you shouldn't have if(br.readLine().equals("Y") || br.readLine().equals("y")) as you're requesting data from the input stream twice if the user actually presses a y the first time. Instead use if(br.readLine().equalsIgnoreCase("Y"))
Last edited by PoovenM; Jan 30th, 2008 at 3:21 am.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 30th, 2008
1

Re: ArrayIndexOutofBoundException

Java Syntax (Toggle Plain Text)
  1. attribute[] attr=new attribute[no_of_attribute];
attr has no_of_attribute arguments
Java Syntax (Toggle Plain Text)
  1. for(int i=0;i<no_of_entity;i++){
i goes from 0 to no_of_entity
Java Syntax (Toggle Plain Text)
  1. for(int j=0;j<no_of_attribute;j++){
j goes from 0 to no_of_attribute

j is never used

Java Syntax (Toggle Plain Text)
  1. System.out.println("The name of the attribute="+this.attr[i].name);
you access attr[i]

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
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Jan 30th, 2008
0

Re: ArrayIndexOutofBoundException

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.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Jan 31st, 2008
0

Re: ArrayIndexOutofBoundException

Click to Expand / Collapse  Quote originally posted by PoovenM ...
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: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
Thanx your suggestion helped me to solve the problem declaring variable at the first
and initializing it into the constructor solved the problem
PS Sorry for late reply
Reputation Points: 10
Solved Threads: 0
Light Poster
arkaprava is offline Offline
34 posts
since Aug 2007
Jan 31st, 2008
0

Re: ArrayIndexOutofBoundException

That is rather interesting... ah such is life that a new day brings new knowledge. P.S. don't forget to mark your post as solved
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help me in Sun Certified Java Associate (SCJA) exam
Next Thread in Java Forum Timeline: Turing Machine Code for Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC