referencing objects via method help
Ok im playing around with inheritence at the moment and ive been trying for the last 3 hours to try work out how to create objects efficiently but in the end my code doesnt work and im sure theres a much better way of doing it.
Below is my code, ill explain after code what it is im trying to acheive and the error.
Code:
import java.util.*;
public class Main {
static catFood filex[] = new catFood[3];
static private int id;
public static void createObject(Object _object, char _size, double _price){
_object = new catFood(++id,_size,_price);
}
public static void main(String[] args) {
createObject(filex[0],'s',4.99);
createObject(filex[1],'m',6.99);
createObject(filex[2],'l',9.99);
System.out.print(filex[0].getSize());
//List lstfilex = Arrays.asList(filex);
}
}
Ok so basically i got an object array called felix and i basically want to fill all the attributes of the object up using a method that i made called createObject.
so i passed the object and the values into the method, but when i compiled it i got a error
Code:
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:24)
Java Result: 1
Now i know why i got this error, its because i got the line
Code:
_object = new catFood(++id,_size,_price);
and it doesnt seem to realise that i am trying to create new instances of the felix objects
so is there a way to fix this?
my ultimate goals are:
1) to get the method in my code above to work and
2) to improve the method so that it can also accept other objects which refence to different classes. so for example rather than refering to new catFood, somehow use the same method to refence dogFood or something.
i got a few ideas but i would like to get some pro feed back, plus this threads getting a bit long.
thanks for reading