954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ArrayIndexOutofBoundException

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;ipublic void getAttributeValues(){

for(int i=0;i

arkaprava
Light Poster
34 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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 :icon_mrgreen: 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 putting attr = new attribute[no_of_attribute]; within your constructor. And simply have attribute 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 :icon_wink:

Oh and it should beAttribute and not attribute as per the Java naming convention :icon_biggrin:

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 thatException

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 

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 ay the first time. Instead use if(br.readLine().equalsIgnoreCase("Y"))

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 
attribute[] attr=new attribute[no_of_attribute];

attr has no_of_attribute arguments

for(int i=0;i<no_of_entity;i++){

i goes from 0 to no_of_entity

for(int j=0;j<no_of_attribute;j++){

j goes from 0 to no_of_attribute

j is never used

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

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

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.

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
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 :icon_mrgreen: 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 putting attr = new attribute[no_of_attribute]; within your constructor. And simply have attribute 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 :icon_wink:

Oh and it should beAttribute and not attribute as per the Java naming convention :icon_biggrin:

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 thatException

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

arkaprava
Light Poster
34 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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 :icon_wink:

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You