We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am making a class for my game that makes a 2d array and loads values from file. i get the error cannot convert 'classname' to int. it doesnt even make sense to try to. the last line is the one with the error.
tilesm1 = loadTable("data/01927647864/94817m1.tcok", "header, csv");
for (TableRow row : tilesm1.rows()) {
tileimg[row.getInt("column")] = loadImage(row.getString("tile"));
tilex[row.getInt("column")] = row.getInt("x");
tiley[row.getInt("column")] = row.getInt("y");
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
map[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
}
}
Answers
this is the actual class
You have class Tiles but the constructor is tiles. They should both be either upper or lower case.
Then, you are using map as a variable name, but map is an existing function. You should probably call it something else.
Makes perfect sense, in line 3 (second code block) you define
map
as a 2D array ofint
(s) then in line 11 (first code block) you create a new Tile object and attempt to store it inmap
which is expecting numbers not objects.Perhaps it should be
Tiles[][] map = new Tiles[20][20];
Well spotted @hamoid - I missed that. Java convention requires class names to start with a capital letter so use
Tiles
It shouldn't cause a problem but it is probably best to rename the array to avoid confusion.
i made the suggested changes, it now returns "cannot find anything named map"
"i made the suggested changes"
Be more explicit...
I made it so both the constructor and class are capitalized. I added this Tiles[][] map = new Tiles[20][20]; appropriately my class is now;
}
but you still have
int[][] map = new int[20][20];
and
map[i][j] = new Tiles(...)
which is assigning Tiles to an int
oh, wait, you also have
Tiles[][] map = new Tiles[20][20];
so map is both a int[][] and a Tiles[][]... won't work...
im not saying that it should work, i just want to know how to make it work. Lets be blunt, How do i make it work?
have only one thing called map, have it the correct type. (ie delete line 3 in the first block of code)
Just a side note: classes and interfaces starting w/ capital letters is just a convention. Java itself doesn't care about that! =P~
But for consistency's sake, I wouldn't even use map as a variable name, since it's a Processing's API function (PApplet)! :-L
i changed the name of int map[][] to int tilemap[][] = new int[][]; now i get cannot convert from Tiles to int
PLEASE, i need a more direct answer.
A Tiles is simply a Tiles data-type, not an
int
or anything else! 8-XHowever, inside Tiles, I see 2
int
fields -> tilex & tiley.Perhaps you wanna copy those 2 fields into 2
int[]
structures? I don't get you! X_XBut why is it trying to convert it to int, i dont want it to be inttttttt where is the problem
Can you re-post it w/ your current modifications?
//PLACES BACKGROUND IMAGES FROM FILE//TILES VARIABLES Table tilesm1;
}
Where are setup() & draw()? I see "global" declarations, then a class, and a drifted away block which belongs to no method! ~:>
Also, why do you declare "global" variables tilex, tiley and tileimg; while at the same time, you got those same fields within Tiles?
That makes your logical very hard to decipher for any1!!! 8-}
And to add for the confusion, you still use map as a variable. I thought you said you had fixed that? [-X
Another Java convention, classes generally use singular names. So instead of Tiles, you'd use Tile!
In line 1 you declare
tilemap
asint [][]
and in line 39 you try to assign aTiles
to it.It looks like you can delete line 7.
All this different advice has gotten very confusing by now, hasn't it?
Yes yes it has, 'GoToLoop', that is because i didnt post the entire code, because it is over 800lines long. Ok here is my new code and a whole new error. "the type of the expression must be an array type but it resolved to PImage". My code
}
//PLACES BACKGROUND IMAGES FROM FILE//TILES VARIABLES Table tilesm1;
Which line # it occurs? And you can't assign an instance of
int[][]
to a Tiles[][] variable!it occurs at tilesmap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
Outside of Tiles, you've declared variables tilex, tiley _& _tileimg as 1D arrays! But you're treating them as 2D there! :@)
I don't know if its's the current issue, but line 1 is still a problem. It should be:
I really hate to keep pestering you people with my newbisms, but thank you that got me one step further, no i have a cannot convert from PImage to PImage[] when i dont even have any PImage[]'s.
}
//PLACES BACKGROUND IMAGES FROM FILE//TILES VARIABLES Table tilesm1;
Tiles tilesmap[][] = new int[20][20];
int[][] tilex = new int[20][20]; int[][] tiley = new int[20][20]; PImage[][] tileimg = new PImage[20][20];
class Tiles { int tilex; int tiley; PImage tileimg;
}
Why don't you post your setup(), so we can have a better idea how you initialize your structures?
Remember to highlight your code and hit CTRL+K to correctly format it! =:)
PImage[] tileimg = new PImage[100];
image(tileimg, tilextilewidth, tileytileheight);
image() cannot take an array as first parameter... You have to choose one PImage out of this array, like:
image(tileimg[index], tilextilewidth, tileytileheight);
where index is an int defined in your class. Or make tileimg a single PImage...
[EDIT] Ah, it is also defined as single image as field in your class! Now, this is really confusing. If you need your array, add a plural to it: tileimgs.
Ok, so i got this almost perfectly working, except for a dreaded Null Pointer exception(the most annoying error possible) Here is my new code for the class and loading it. Sorry about the bad formating, it doesnt like this class. And ctrl + K just does the same thing as alt +tab on my computer LOL.
You can also click at C panel button. It's the same as CTRL+K!
It just indents highlighted text 4 spaces to the right. This way, forum knows it's a code block!
Anyways, you should follow this scheme for your program:
It'd help us a lot if we could see your entire setup() block!
All global variables associated with the code in question are accounted for. The reason i dont want to post those is because to give you functional code it would be 830 lines long and noone wants to read that. The reason i have it padted the way i do is because the 3 seperated code blocks are in different tabs(files). If you still request i post the setup and draw i will. But i hesitate because it calls 10odd functions that are not included.
How big your setup()? I didn't request your draw() block!
Here, please refrain from giving advice on things that are not reguarding the "Tiles" class.
The error is NullPointerException, absolute worst thing ever invented.
mmmm please anyone?
Why do I get a NullPointerException?
Actually, NPE is often one of the easiest exceptions to address: you get a line where it happens, and generally it is because you forgot to init something, so looking at the line often tells what object is null. You can also println() them, to see which one is null.
Since we cannot run your code, and you don't tell us the line in fault, it is hard to help you more.
It does not give me a line in fault. Trust me i would tell you if i knew.
In the code
the program stops on the image() call, highlighting the line where the error happens.
If you have code on several tabs, perhaps the line isn't immediately visible?
If you have a stack trace (several lines in the output pane), paste it here.
is this what you were reffering to? Also i checked every tab for a highlighted line, and there still are none.
OK, as I feared, it is an internal error (?). Although the output is strange... Is it all you have?
Some remarks on your setup():