-1

Possible Duplicate:
non-static variable cannot be referenced from a static context (java)

I am trying to create more than one objects (in this case cars) of the same class and then I am trying to check if c1 (the name of the object) was created (knowing that it was) and then check if c2 (other object of the same class) was created (knowing that it wasn't). I've created the class Car: package parkingLot;

/**
 *
 * @author HASLima
 */
public class Car {

String brand;
String plates;
int mileage;

public String getMarca() {
    return brand;
}

public void setMarca(String brand) {
    this.brand = brand;
}

public String getMatricula() {
    return plates;
}

public void setMatricula(String plates) {
    this.plates = plates;
}

public int getKilometros() {
    return mileage;
}

public void setKilometros(int mileage) {
    this.mileage = mileage;
}


}

And then created the class Park: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package parkingLot;

/**
 *
 * @author HASLima
 */
public class Park {
    int nrOfCars;
    int space;
    Car[] c;
    int a = 0;

    public Park (int nrOfPlaces){
        space = nrOfPlaces;
        nrOfCars = 0;

     }
    public static void main(String[] args) {
        Park park1 = new Park(5);
        c[a] = new Car();
    }
}

And here is the problem, the

c[a] = new Car();

Returns this error:

non-static variable c cannot be referenced from a static context and non-static variable a cannot be referenced from a static context

Community
  • 1
  • 1
Hélder Lima
  • 87
  • 1
  • 1
  • 7

2 Answers2

3

The array Car[] c is defined as an object variable of Park therefore you must use park1.c[a] instead of trying to call the reference c[a]

Also variable a is an Object of Park and you can't reference it from main. It should be park1.a or better yet wrapped the variable with a getter park1.getA()

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • The stange thing is that I have done something similar to this but I didn't have to use nothing before the c and nothing before the a. But what you said works so, perfect – Hélder Lima Sep 29 '12 at 22:06
  • @HélderLima I'll recommend reading about Variable Scope http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm and about Classes and Instances of Classes http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – gtgaxiola Sep 29 '12 at 22:07
  • Now I get this: Exception in thread "main" java.lang.NullPointerException at parkingLot.Park.main(Park.java:24) Java Result: 1 – Hélder Lima Sep 29 '12 at 22:20
  • @HélderLima hard to determine since I don't know what Line 24 in Park.java is and your new edits in your code – gtgaxiola Sep 29 '12 at 22:22
  • line 24: park1.c[park1.a] = new Car(); – Hélder Lima Sep 29 '12 at 22:24
  • @HélderLima park1.c is not initalized you must initalized your Array – gtgaxiola Sep 29 '12 at 22:26
1

non static variable cant be accessed inside static methods unless you call them on their instance. In your case as c[] is an instance variable which is not static and you are accessing it inside static method youshould be accessing it on park instanc like this:

Park park1 = new Park(5);
 park1.c[park1.a] = new Car();
PermGenError
  • 45,977
  • 8
  • 87
  • 106