FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Vectors and Objects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Vectors and Objects  (Read 290 times)
spectre_240sx

Email
Vectors and Objects
« on: Apr 2nd, 2004, 10:53pm »

I posted earlier looking for information on object naming and management. One person suggested using vectors, and after some research I agree that that would be the best way to go. Unfortunately, I've had some trouble finding examples of using vectors with objects and Sun's site wasn't very helpful either.  
 
I think I have it mostly figured out, but I'm still getting one error constantly and I have no idea what's going on. Here's some sample code of basically what I'm trying to do.
 
Code:

import java.util.Vector;
 
public class test extends BApplet {
 
   
 
  void setup() {
   
  size(100, 100);
   
  //person objPerson = new person("Indiana Jones", "Blue", "Cincinatti");
   
  //println(objPerson.name);
  //println(objPerson.eyecolor);
  //println(objPerson.location);
   
  Vector objPerson = new Vector();
   
  objPerson.addElement(new person("Mack Daddy", "Pink", "The Big Easy"));
  objPerson.addElement(new person("Funkmaster Flash", "White", "Brooklyn"));
   
  println(objPerson.get(1).name);
   
  }
 
  public class person extends Object {
   
    String name;
    String eyecolor;
    String location;  
 
     
    //constructor  
    public person(String name, String eyecolor, String location ) {
     
    this.name = name;    
    this.eyecolor = eyecolor;
    this.location = location;
     
    }
     
   
  }
 
}

 
I'm recieving this error message when I try to run the code.
 
 
Quote:

Semantic Error: No field named "name" was found in type "java.lang.Object"

 
I would be most greatful if someone could shed some light on the subject .  
 
Thanks in advance.
 
fry


WWW
Re: Vectors and Objects
« Reply #1 on: Apr 3rd, 2004, 1:59am »

a Vector will only store things as generic Object classes. so when you do  
objPerson.get(1);
the type that comes out is an Object class, not a Person class. you need to 'cast' it to a Person class:
 
person p = (person) objPerson.get(1);
println(p.name);
 
or smooshed together:
 
println(((person) objPerson.get(1)).name);
 
you also don't need the 'import' and 'public class' lines at the top.. your code would work just fine without it. especially if you drop the 'public' from the 'public class person' inner class.
 
Koenie

170825270170825270koeniedesign WWW Email
Re: Vectors and Objects
« Reply #2 on: Apr 4th, 2004, 9:16pm »

Maybe the problem is in the naming of the variables in your person class. Maybe you can try this (it's just putting an 'n' in front of the variable names brought in by the constructor):
 
Code:
public class person extends Object {
  String name;  
  String eyecolor;  
  String location;  
 
  public person(String nname, String neyecolor, String nlocation) {
    name = nname;
    eyecolor = neyecolor;
    location = nlocation;
  }
}

 
Koenie
 

http://koeniedesign.com
spectre_240sx

Email
Re: Vectors and Objects
« Reply #3 on: Apr 4th, 2004, 9:17pm »

Sweet, that makes so much sense now. Yeah, I wasn't sure about the public declarations although the vector class wasn't working for me when I didn't explicitly import it.  
 
I've been trying to find some documentation that would give me a decent idea of when things should be public, private or protected, but I haven't had much luck yet, lol.  
 
thanks!
 
Pages: 1 

« Previous topic | Next topic »