Class, cannot convert 'classname' to int

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]);
  }
}
Tagged:

Answers

  • edited November 2013
      Table tilesm1;
    
      int[][] map = new int[20][20];
    
      int[] tilex = new int[20];
      int[] tiley = new int[20];
      PImage[] tileimg = new PImage[100];
    
      class Tiles {
        int tilex;
        int tiley;
        PImage tileimg;
    
    
        tiles(PImage t, int x, int y) {
          tilex = x;
          tiley = y;
          tileimg = t;
        }
    
        void render() {
          image(tileimg, tilex*tilewidth, tiley*tileheight);
        }
      }
    

    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 of int(s) then in line 11 (first code block) you create a new Tile object and attempt to store it in map which is expecting numbers not objects.

    Perhaps it should be

    Tiles[][] map = new Tiles[20][20];

  • You have class Tiles but the constructor is tiles. They should both be either upper or lower case.

    Well spotted @hamoid - I missed that. Java convention requires class names to start with a capital letter so use Tiles

    map is an existing function

    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"

        map[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
    
  • "i made the suggested changes"
    Be more explicit...

  • edited November 2013

    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;

      Table tilesm1;
    
      int[][] map = new int[20][20];
    
      int[] tilex = new int[20];
      int[] tiley = new int[20];
      PImage[] tileimg = new PImage[100];
    
      Tiles[][] map = new Tiles[20][20];
    
      class Tiles {
        int tilex;
        int tiley;
        PImage tileimg;
    
    
        Tiles(PImage t, int x, int y) {
          tilex = x;
          tiley = y;
          tileimg = t;
        }
    
        void render() {
          image(tileimg, tilex*tilewidth, tiley*tileheight);
        }
      }
    
  • //TILES
    //LOAD TILE LOCS
    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]);
      }
    }
    

    }

  • 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)

  • edited November 2013

    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.

  • edited November 2013

    A Tiles is simply a Tiles data-type, not an int or anything else! 8-X
    However, 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_X

  • But 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;

    int[][] tilemap = new int[20][20];
    
    int[] tilex = new int[20];
    int[] tiley = new int[20];
    PImage[] tileimg = new PImage[100];
    
    Tiles[][] map = new Tiles[20][20];
    
    class Tiles {
      int tilex;
      int tiley;
      PImage tileimg;
    
    
      Tiles(PImage t, int x, int y) {
        tilex = x;
        tiley = y;
        tileimg = t;
      }
    
      void render() {
        image(tileimg, tilex*tilewidth, tiley*tileheight);
      }
    }
    
    
    //TILES
    //LOAD TILE LOCS
    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++) {
        tilemap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
      }
    }
    

    }

  • 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 as int [][] and in line 39 you try to assign a Tiles 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

    //TILES
    //LOAD TILE LOCS
    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++) {
        tilesmap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
      }
    }
    

    }


    //PLACES BACKGROUND IMAGES FROM FILE//TILES VARIABLES Table tilesm1;

    Tiles tilesmap[][] = new int[20][20];
    
    int[] tilex = new int[20];
    int[] tiley = new int[20];
    PImage[] tileimg = new PImage[100];
    
    class Tiles {
      int tilex;
      int tiley;
      PImage tileimg;
    
    
      Tiles(PImage t, int x, int y) {
        tilex = x;
        tiley = y;
        tileimg = t;
      }
    
      void render() {
        image(tileimg, tilex*tilewidth, tiley*tileheight);
      }
    }
    
  • edited November 2013

    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]);

  • edited November 2013

    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:

    Tiles tilesmap[][] = new Tiles[20][20];
    
  • edited November 2013

    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.

    //TILES
    //LOAD TILE LOCS
    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++) {
        tilesmap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
      }
    }
    

    }


    //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;

    Tiles(PImage t, int x, int y) {
      tilex = x;
      tiley = y;
      tileimg = t;
    }
    
    void render() {
      image(tileimg, tilex*tilewidth, tiley*tileheight);
    }
    

    }

  • 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! =:)

  • edited November 2013

    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.

  • edited November 2013

    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.

      Table tilesm1;
    
      Tiles tilesmap[][] = new Tiles[20][20];
    
      PImage[][] tileimg = new PImage[20][20];
      int[][] tilex = new int[20][20];
      int[][] tiley = new int[20][20];
    
      class Tiles {
        int tilex;
        int tiley;
        PImage tileimg;
    
    
        Tiles(PImage t, int x, int y) {
          tilex = x;
          tiley = y;
          tileimg = t;
        }
    
    
        void render() {
          image(tileimg, tilex*tilewidth, tiley*tileheight);
        }
      }
    
    -----------------------------------------------------------------------
    
        //OBJECTS
        for (int i = 0; i < 20; i ++ ) {
          for (int j = 0; j < 20; j ++) {
            tilesmap[i][j].render();
          }
        }
    
    -----------------------------------------------------------------------
    
        //TILES
        //LOAD TILE LOCS
        tilesm1 = loadTable("data/01927647864/94817m1.tcok", "header, csv");
    
        for (TableRow row : tilesm1.rows()) {
          tileimg[row.getInt("column")] [0]= loadImage(row.getString("tile"));
          tilex[row.getInt("column")] [0]= row.getInt("x");
          tiley[row.getInt("column")] [0]= row.getInt("y");
        }
    
        for (int i = 0; i < 20; i++) {
          for (int j = 0; j < 20; j++) {
            tilesmap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
          }
        }
      }
    
  • 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:

    • "Global" variable declarations.
    • setup() block.
    • draw() block.
    • event callbacks.
    • own functions.
    • classes/interfaces.

    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.

  • Answer ✓

    How big your setup()? I didn't request your draw() block!

  • edited November 2013

    Here, please refrain from giving advice on things that are not reguarding the "Tiles" class.

          void setup() {
          size(1024, 768, OPENGL);
          frame.setResizable(true);
          imageMode(CORNER);
          noStroke();
          smooth();
          //SPRITES
          char1 = loadImage("char1.png");
          char12 = loadImage("char1-2.png");
          char13 = loadImage("char1-3.png");
          char2 = loadImage("char2.png");
          char22 = loadImage("char2-2.png");
          char23 = loadImage("char2-3.png");
          char3 = loadImage("char3.png");
          char32 = loadImage("char3-2.png");
          char33 = loadImage("char3-3.png");
          char4 = loadImage("char4.png");
          char42 = loadImage("char4-2.png");
          char43 = loadImage("char4-3.png");
          start = loadImage("startpage.png");
          //BUTTONS
          buttonnew = loadImage("buttonnew.png");
          buttoncontinue = loadImage("buttoncontinue.png");
          buttonsave = loadImage("buttonsave.png");
          buttonstats = loadImage("buttonstats.png");
          //TILES
          forestcornertl = loadImage("forest cornertl.png");
          forestcornertr = loadImage("forest cornertr.png");
          forestcornerbl = loadImage("forest cornerbl.png");
          forestcornerbr = loadImage("forest cornerbr.png");
          forestwallt = loadImage("forest wallt.png");
          forestwalll = loadImage("forest walll.png");
          forestwallr = loadImage("forest wallr.png");
          forestwallb = loadImage("forest wallb.png");
          rubblegrasscornertl = loadImage("rubble grass cornertl.png");
          rubblegrassforest = loadImage("rubble grass forest.png");
          rubble = loadImage("rubble.png");
          //MISC, ART
          escimage = loadImage("escimage.png");
          statsmenu = loadImage("statsmenu.png");
          loading = loadImage("loading.png");
    
          //loadhouses
          obj = loadTable("data/01927647864/6783.tcok", "header, csv");
    
          for (TableRow row : obj.rows()) {
            house[row.getInt("column")] = loadImage(row.getString("house"));
            housex[row.getInt("column")] = row.getInt("housex");
            housey[row.getInt("column")] = row.getInt("housey");
            housew[row.getInt("column")] = row.getInt("housew");
            househ[row.getInt("column")] = row.getInt("househ");
          }
    
          for (int i = 0; i < 100; i ++ ) { // Initialize each Object using a for loop.
            objects[i] = new Object(house[i], housex[i], housey[i], housew[i], househ[i]);
          }
    
          //TILES
          //LOAD TILE LOCS
          tilesm1 = loadTable("data/01927647864/94817m1.tcok", "header, csv");
    
          for (TableRow row : tilesm1.rows()) {
            tileimg[row.getInt("column")] [0]= loadImage(row.getString("tile"));
            tilex[row.getInt("column")] [0]= row.getInt("x");
            tiley[row.getInt("column")] [0]= row.getInt("y");
          }
    
          for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
              tilesmap[i][j] = new Tiles(tileimg[i][j], tilex[i][j], tiley[i][j]);
            }
          }
        }
    
  • The error is NullPointerException, absolute worst thing ever invented.

  • edited November 2013

    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

    PImage img;
    
    void setup()
    {
      size(800, 800);
    }
    
    void draw()
    {
      background(255);
      image(img, 0, 0);
    }
    

    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.

  • edited November 2013
    java.lang.NullPointerException
        at processing.mode.java.runner.Runner.findException(Runner.java:926)
        at processing.mode.java.runner.Runner.reportException(Runner.java:871)
        at processing.mode.java.runner.Runner.exceptionEvent(Runner.java:797)
        at processing.mode.java.runner.Runner$2.run(Runner.java:686)
    

    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():

    • Why do you run in OpenGL mode? Is your sketch really 3D? If not, perhaps try and run in default mode, some people have issues with their graphics card and OpenGL.
    • You have a class named Object? Bad idea, it is the name of the fundamental class that every other class in Java extends. I suggest to find another name, even if this name can be safe (local?): it will be less confusing.
Sign In or Register to comment.