Search code examples
javavector

cannot find symbol, vector of classes


In my code I'm using a vector which holds Reservation objects.

Reservation class:

public class Reservation implements java.io.Serializable {
int ano;
int bno;
int cno;
int dno;
int eno;
String name;

While compiling the following error occurs:

error: cannot find symbol
`System.out.println("Type A = "+ vec.get(0).ano +",price: 75 euro\\n");`
.ano
^
symbol:   variable ano
location: class Object

This error occurs every time when similar syntax is used (vec.get().example).

Sample code for reference:

import java.util.Vector;

public class HRImpl extends java.rmi.server.UnicastRemoteObject implements HRInt {
    public HRImpl() throws java.rmi.RemoteException 
    {
        super();
    }
    //Vector<Reservation> vec = new Vector<Reservation>();
    Reservation temp = new Reservation(0);

    public Vector list(Vector vec) throws java.rmi.RemoteException 
    {
        System.out.println("Available rooms:\n");
        System.out.println("Type A = "+ vec.get(0).ano +",price: 75 euro\n");
        System.out.println("Type B = "+ vec.get(0).bno +",price: 110 euro\n");
        System.out.println("Type C = "+ vec.get(0).cno +",price: 120 euro\n");
        System.out.println("Type D = "+ vec.get(0).dno +",price: 150 euro\n");
        System.out.println("Type E = "+ vec.get(0).eno +",price: 250 euro\n");
        return vec;
  }

Solution

  • The code in the question is incomplete.

    But despite this, I believe you are misunderstanding how Java collections work. A Vector, in the way you are using it, is capable of storing any kind of object. You can put

    • Integer
    • String
    • Car
    • Rocket
    • Reservation

    in the Vector. So the compiler does not let you write a reference to a field like ano which does not exist in general on any Object. So you must cast the item from the Vector getter call, e.g.:

    ((Reservation) vec.get(0)).ano
    

    You should read a tutorial on java generics.

    (Oh and you should switch to using an ArrayList rather than a Vector.)