We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › No accessible field named... error
Page Index Toggle Pages: 1
No accessible field named... error (Read 604 times)
No accessible field named... error
Nov 24th, 2007, 11:14pm
 
I have a compilation problem in Processing with syntax which works fine in java. I cant't understand where comes the problem from.

I have a class Phiosopher with a constructor in separate file:

class Philosopher
{

public String name;

public int yearsFrom;

public int yearsTo;

public ArrayList other;


Philosopher() //default constructor

{


name = "";


yearsFrom = 0;


yearsTo = 0;


other = new ArrayList();

}
       public void setName(String name)
       {
         this.name = name;
       }
       public String getName()
       {
         return this.name;  
       }
}

then i have a method which parses a file and returns ArrayList of Philosopher(s).
(Thing that bothers me also is that i get compilation error if using <Philosopher> generic). anyways the codde snippet:

void setup()
{
   ArrayList p = new ArrayList();  
   p = readFile("philosophers.txt");  //this fills ArrayList with Philosophers

   print(p.get(0).name); //this produces  an compilation error! :
Semantic Error: No accessible field named "name" was found in type "java.lang.Object".



}

it does also produce an error if i handle object Philosopher via getters and setters:

   ArrayList p = new ArrayList();    
   p = readFile("phil.txt");
   print(p.get(0).getName());

...will produce: Semantic Error: No accessible method with signature "getName()" was found in type "java.lang.Object".


or:

   ArrayList p = new ArrayList();    
   p = readFile("phil.txt");

   Philosopher phil = new Philosopher();
   phil = p.get(0);

   print(phil.getName());

will produce: Semantic Error: The type of the right sub-expression, "java.lang.Object", is not assignable to the variable, of type "Temporary_3414_9666$Philosopher".

I'm sure, that ArrayList is filled properly, since

print(p.get(0));

will return Temporary_5388_3296$Philosopher@74c3aa


Any suggestions?
Re: No accessible field named... error
Reply #1 - Nov 25th, 2007, 1:57am
 
ArrayList stored everything as Object, not your actually class type, so what you get from p.get(0) isn't a Philosopher, it's an Object, and youll have to cast it to get at the "name" field:

Code:
//either:
print(((Philosopher)p.get(0)).name);
//or
Philosopher phil=(Philosopher)p.get(0);
print(phil.name);
Re: No accessible field named... error
Reply #2 - Nov 25th, 2007, 2:21am
 
Thanks. I found that out after i made a post. But it's strange since in eclipse thing works fine with java 6 SE. Is Processing's compiler that bad?
Re: No accessible field named... error
Reply #3 - Nov 25th, 2007, 11:37am
 
Processing usese Java 1.4, in 1.5 and higher, you can make specfic type ArrayList, so insted of every ArrayList just storing "Object" they store a specific type (but are defined differently, e.g. ArrayList<Philosopher> to define a Philosopher ArrayList)

In 1.4 there's no such form, so no matter what program you use, if it's working with Java 1.4 you'll have to do casting and the like.
Re: No accessible field named... error
Reply #4 - Nov 25th, 2007, 1:58pm
 
mogwai wrote on Nov 25th, 2007, 2:21am:
Thanks. I found that out after i made a post. But it's strange since in eclipse thing works fine with java 6 SE. Is Processing's compiler that bad

http://processing.org/faq.html#java
http://processing.org/reference/environment/index.html#Tabs
Page Index Toggle Pages: 1