Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ok I don't get this  (Read 1937 times)
0 Members and 1 Guest are viewing this topic.
Offline AwexGreg

Junior Newbie





« Posted 2009-10-10 21:46:52 »

Hi all,

I've got this in my code:

            if(main.activeLevel.itemMap[fp.hor][fp.ver]!=null)

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?

Thanks.
Offline cylab

JGO Ninja


Medals: 55



« Reply #1 - Posted 2009-10-10 21:56:07 »

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.

Mathias - I Know What [you] Did Last Summer!
Offline JL235

JGO Coder


Medals: 10



« Reply #2 - Posted 2009-10-10 22:45:02 »

At a guess, itemMap might be a 1 dimensional array when you are using it like a 2d array.

Also you shouldn't access variables directly. You should write accessor methods for getting the values out.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2009-10-11 07:55:22 »

Also you shouldn't access variables directly. You should write accessor methods for getting the values out.

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.
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2009-10-11 12:12:10 »

At a guess, itemMap might be a 1 dimensional array when you are using it like a 2d array.

Also you shouldn't access variables directly. You should write accessor methods for getting the values out.

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.

Play Minecraft!
Offline fletchergames

Senior Devvie





« Reply #5 - Posted 2009-10-11 20:43:33 »

Hi all,

I've got this in my code:

            if(main.activeLevel.itemMap[fp.hor][fp.ver]!=null)

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?
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:

1  
Item[][] itemMap = new Item[mapWidth][mapHeight];


It may be that you declared the variable wrong.

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.
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:

1  
2  
3  
boolean isThereAnItemAt(int x, int y) {
   return itemMap[x][y] != null;
}


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.
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2009-10-12 07:24:25 »

The difference is developing a public API, or a private API. In a private API, use default access and no getter methods if you like.

Offline JL235

JGO Coder


Medals: 10



« Reply #7 - Posted 2009-10-12 08:27:50 »

The difference is developing a public API, or a private API. In a private API, use default access and no getter methods if you like.
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.

Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2009-10-12 08:58:19 »

Good point! I agree with that.

Play Minecraft!
Offline pjt33
« Reply #9 - Posted 2009-10-12 11:41:21 »

The difference is developing a public API, or a private API. In a private API, use default access and no getter methods if you like.
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?)

But this is getting rather off-topic.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!