Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

an ArrayList Problem

  Asked By: Vernon    Date: Jul 01    Category: Java    Views: 801
  

I'm getting the error:
non-static variable arrayInxObject cannot be referenced from a static
context
On the following line of code:
junkData.set(y, xObject.arrayInxObject[column] = z);

Could someone please point out to me the exact problem, and possibly a
solution

The below is a sample of the code this line was taken from:

public class Junk
{
public static final int[] SIZE = {5, 10, 20, 25, 30, 35, 40, 45,
50, 55};
private ArrayList junkData;
private ReaderObject reader;

public Junk()
{
importData();
}

public void importData()
{
reader = new ReaderObject();
junkData = new ArrayList();
for(int i = 0; i < 10; i++)
{
int x = SIZE[i];
junkData.add(i, new xObject(x));
}
for(int y = 0; y < 10; y++)
{
double z = 0;
double temporaryArray[] = reader.getOneLine();
for(int column = 0; column < temporaryArray.length; column++)
{
z = temporaryArray[column];
junkData.set(y, xObject.arrayInxObject[column] = z);
}
}
}

// other methods removed

}

public class xObject
{
public double[] arrayInxObject;

public xObject(int size)
{
arrayInxObject = new double[size];
}

// other methods removed
}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Earl Stone     Answered On: Jul 01

arrayInxObject is an instance variable  of object xObject and cannot be
accessed by using the class  name xObject. Try something like this:

xObject myxObject = new xObject(x);
myxObject.arrayInxObject[column] = z;

 
Answer #2    Answered By: Anna Hill     Answered On: Jul 01

Thanks to those who helped with my last question, I've now managed to
put all the relevent objects into the ArrayList with the following:

public void  importData()
{
reader = new ReaderObject();
junkData = new ArrayList();
for(int i = 0; i < 10; i++)
{
int x = SIZE[i];
Xobject objectX = new Xobject(x);
double z = 0;
double temporaryArray[] = reader.getOneLine();
for(int column  = 0; column < temporaryArray.length; column++)
{
z = temporaryArray[column];
objectX.arrayInXobject[column] = z;
}
junkData.add(i, objectX);
}
}

But now how do I access the methods  inside of these objects in the
ArrayList ? This code  doesn't work:

public double  someMethod(int x, int  y)
{
this.x = x;
this.y = y;
double z = 0.0;
z = junkData.get(x, Xobject.anotherMethod(y));
return z;
}

Could someone please help me to fix this.

 
Didn't find what you were looking for? Find more on an ArrayList Problem Or get search suggestion and latest updates.




Tagged: