The compiler complains about it. It says: array required but ItemMap found. It would not compile. ItemMap is an array (obviously), holding references to Item objects. What I’m trying to do in this line is to check whether the Item object is null or not. Why is this line not valid, can someone please tell me or should I just simply call the mental hospital as I’m going nuts?
Check your definition of itemMap. It very likely isn’t an array or is not accessible for some reason. In doubt paste the code of the class your itemMap is in.
This is subjective. My take on it: a private member with public getters and setters that do nothing other than get and set = a waste of time and effort, especially if you are the only one to use the code.
Back on topic - we can’t really diagnose without seeing the declaration for itemMap.
This is waaaay off topic, but I keep hearing people say this, and I disagree. It’s very very rare for me to run into a situation where I wish I’d used accessors instead of direct member access, and when I do, it’s very easy to change it to an accessor (takes less than a minute in eclipse).
I find player.getAge() less readable than player.age, and avoiding millions of accessor methods makes classes much smaller and easier to navigate as well.
Could you post your declaration of of the itemMap variable and the line you allocate the memory (if it’s on a different line)? That might help indicate the problem. The line should look something like:
Item[][] itemMap = new Item[mapWidth][mapHeight];
It may be that you declared the variable wrong.
I think get and set methods try and fail to address a real problem. When you have a variable (like itemMap for instance), you should often try to avoid accessing it outside the class it’s created in. If you define methods within that class that use the itemMap variable, then you don’t need expose the variable throughout the program.
In the case described above, there should probably be a method of the form:
This method should be included in the Map class (if there is such a class). Alternatively, AwexGreg might be able to move the whole method he was writing into the Map class. This wouldn’t fix his problem, but his code would be more modular.
So, IMO, get and set methods usually just hide the problem rather than fix it.
Agreed. In a public API it’s really bad idea to expose variables because it cements the implementation to the API and so makes the implementation harder to change.
But it’s easier to move code from being private to public if they were built to the same standards.
And in a public API use OO design whereby methods are instructions to do something rather than simple requests for data. The only classes which should allow access to data they contain are structs, which should be private or immutable. (Yes, I am anti-beans; how did you guess?)