Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    Clicking the multiplied tile


    Problem: When i open game i get NullpointErrorException out of boolean.
    What i want: I want the game console keep telling me: has the screen been clicked (false or true). if the screen is clicked then it should go true once

    There are 2 different tests what seems to logically the best ones (yet none of them works like i want), and i will post one of the tests (inside spoiler, hope you see the codes)

    First test:
    In the console it keeps telling me that screen has not been clicked:
    console message:
    Code:
    false
    false
    false
    false
    ..so on
    but when i click on screen, it still says false

    When i change the code order in Game.class update method, i get this error:

    Exception in thread "Display" java.lang.NullPointerException
    at Tiles.GrassTile.update(GrassTile.java:35)
    at EpoxGame.Game.update(Game.java:136)
    at EpoxGame.Game.run(Game.java:108)
    at java.lang.Thread.run(Unknown Source)


    here are the codes for first test. (i deleted all the comments and methods i don't use for this problem or in program at all to keep it less messy for you)
    (i also try to explain what this class is ment to do, even though i might fail at it, because i will explain it my own way not in the programming way, explenations are inside the spoilers)
    [spoiler=test1]
    Level.java
    [spoiler=explanation]
    In this class im creating an array of boxes and it inserts Tile* to the box depending on what color is the box
    Tile* = GrassTile or StoneTile or WaterTile
    im not sure does it insert the Tile effects too in the array or not (effects like MouseHandler and Keyboard inputs), but i hope it does.
    [/spoiler]
    [spoiler=LevelCode]
    Code:
    package Map;
    
    import EpoxGame.MouseHandler;
    import Graafika.Screen;
    import Tiles.Tile;
    
    public class Level {
    
    		
    		protected int width, height;
    		protected int[] tilesInt;
    		protected int[] tiles;
    		
    		public Level (int width, int height) {
    			this.width = width;
    			this.height = height;
    			tilesInt = new int[width*height];
    			generateLevel();
    	}
    
    		public Level(String path) {
    			loadLevel(path);
    			generateLevel();
    		}
    		
    		protected void generateLevel() {
    		}
    		
    		protected void loadLevel(String path){
    		
    		}
    		
    		public void update(){
    		}
    		
    		public void render(int xScroll, int yScroll, Screen screen) {
    			screen.setOffset(xScroll, yScroll);
    			int x0 = xScroll>>4; //>>4 = 16 , sest 4x4=16
    			int x1 = (xScroll + screen.width+16)>>4;
    			int y0 = yScroll>>4;
    			int y1 = (yScroll + screen.height+16)>>4;
    			
    			for (int y=y0; y<y1; y++){
    				for (int x=x0; x<x1; x++){
    					getTile(x, y).render(x, y, screen);
    				}
    			}
    		}
    		// Grass = 0xff 00ff0c
    		// Water = 0xff 005aff
    		// Rock = 0xff 7e817f
    		public Tile getTile(int x, int y){
    			if (x<0 || y<0 || x>= width || y>= height) return Tile.voidtile;
    			if (tiles[x+y*width] == 0xff00ff0c) return Tile.grass;
    			if (tiles[x+y*width] == 0xff7e817f) return Tile.stone;
    			if (tiles[x+y*width] == 0xff005aff) return Tile.water;
    			return Tile.voidtile;
    		}
    }
    [/spoiler]

    Tile.java
    [spoiler=explanation]
    This should be the SuperClass for all the Tiles. The hirearchy thing, everything this class has will be on the subclasses of this class too.
    Level class gets his tiles from this class.
    [/spoiler]
    [spoiler=TileCode]
    Code:
    package Tiles;
    
    import EpoxGame.MouseHandler;
    import Graafika.Screen;
    
    public abstract class Tile {
    	
    	public int x,y;
    	public static MouseHandler click;
    
    	public static Tile grass = new GrassTile();
    	public static Tile stone = new StoneTile();
    	public static Tile water = new WaterTile();
    	public static Tile voidtile = new voidTile();
    	
    	public void update(){
    	}
    
    	public void render (int x, int y, Screen screen) {
    	}
    	
    }
    [/spoiler]

    GrassTile.java
    [spoiler=explanation]
    This is one of the Tiles*. Right now it renders the picture on to screen (Tile will get location for picture from the level.class).
    Also im trying to give him boolean value. BUT ITS ERRORS ALL THE WAY. (when i create internal booleans(inside tile or or his subclasses), they will work)
    [/spoiler]
    [spoiler=GrassTileCode]
    Code:
    package Tiles;
    
    import Graafika.Pilt;
    import Graafika.Screen;
    
    public class GrassTile extends Tile {
    	
    	public GrassTile() {
    	}
    
    	public void update(){
    		System.out.println(Tile.click.clicked);
    	}
    
    	public void render (int x, int y, Screen screen) {
    		screen.renderTile(x<<4, y<<4, Pilt.grass);
    }
    [/spoiler]

    Game.java
    [spoiler=explanation]
    well idk how to explain this class, since not sure about that class myself, i think its the main "game engine" where every class will be connected.
    this time i didn't delete much of the lines since who knows what might bring up the problem.
    [/spoiler]
    [spoiler=GameCode]
    Code:
    package EpoxGame;
    
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    
    import javax.swing.JFrame;
    
    import Graafika.Screen;
    import Kollid.Zombie_Oleshion;
    import Map.Level;
    import Map.SpawnLevel;
    import Tiles.GrassTile;
    import Tiles.Tile;
    import WindowPildid.ProtoClock;
    
    public class Game extends Canvas implements Runnable{
    	private static final long serialVersionUID = 1L;
    
    	public static int width = 320;
    	public static int height = 200;
    	public static int scale = 3;
    	
    	private Thread thread;
    	private JFrame frame;
    	private InputHandler key;
    	private MouseHandler click;
    	private Level level;
    	
    	private Tile tile;
    	
    	private Zombie_Oleshion Unit_Zombie_Oleshion;
    	
    	private ProtoClock clock;
    
    	//-------------------------------------------------------
    	private boolean running = false;
    	private Screen screen;
    	
    	private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
    
    	
    	public Game(){
    		setMinimumSize(new Dimension(width*scale, height*scale));
    		setMaximumSize(new Dimension(width*scale, height*scale));
    		setPreferredSize(new Dimension(width*scale, height*scale));		
    		
    		screen = new Screen(width, height);
    		frame = new JFrame();
    		level = new SpawnLevel("/Level.png");
    		
    		key = new InputHandler();
    		addKeyListener(key);
    		click = new MouseHandler();
    		
    		clock = new ProtoClock(x, y, key);
    
    		Unit_Zombie_Oleshion = new Zombie_Oleshion(x, y, click, key);
    
    		addMouseListener(click);
    		addMouseMotionListener(click);	
    
    		tile = new GrassTile();
    
    	}
    
    
    	public synchronized void start(){
    		running = true;
    		thread  = new Thread(this, "Display");
    		thread.start();
    	}
    	public synchronized void stop(){
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void run() {
    		long lastTime = System.nanoTime();
    		long timer = System.currentTimeMillis();
    		final double nsPerTick = 1000000000D/60D;
    		double delta = 0;
    		int updates = 0;
    		int frames = 0; 
    			
    		requestFocus();
    		
    		while (running) {
    			long now = System.nanoTime();
    			delta += (now - lastTime) / nsPerTick;
    			lastTime = now;
    			while (delta >=1){
    				update();
    				updates++;
    				delta--;}
    			
    			render();
    			frames++;
    			
    			if (System.currentTimeMillis() - timer > 1000){
    				timer += 1000;
    				//System.out.println(updates + " ticks, " + frames + " frames");
    				frames = 0;
    				updates = 0;}}
    		stop();
    		}
    		
    	int x=0, y=0;
    	private void update() {
    		key.update();
    		if (key.up) y--;
    		if (key.down) y++;
    		if (key.left) x--;
    		if (key.right) x++;
    		clock.update();
    		Unit_Zombie_Oleshion.update();
    
    		click = new MouseHandler();
    
    		Tile.click = click;
    		Tile.grass.update();
    	}
    	
    	private void render() {
    		BufferStrategy bs = getBufferStrategy();
    		if (bs == null){
    			createBufferStrategy(3);
    			return;}
    		
    		screen.clear();
    		
    		level.render(x, y, screen);
    		Unit_Zombie_Oleshion.render(screen);
    		clock.render(screen);
    		
    		for (int i=0; i<pixels.length; i++) {
    			pixels[i] = screen.pixels[i];
    		}
    			
    		Graphics g = bs.getDrawGraphics();
    		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    		g.dispose();
    		bs.show();
    	}
    
    
    	public static void main(String[] args){
    		Game game = new Game();
    		game.frame.setResizable(false);
    		game.frame.setTitle("Epox");
    		game.frame.add(game);
    		game.frame.pack();
    		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.frame.setLocationRelativeTo(null);
    		game.frame.setVisible(true);
    		
    		game.start();
    	}
    }
    [/spoiler]

    MouseHandler.java
    [spoiler=explanation]
    This thing will only get the locations where i clicked and print it out on my console and
    if i click on my screen it will turn boolean clicked= true for that brief moment.
    [/spoiler]
    [spoiler=MouseHandlerCode]
    Code:
    package EpoxGame;
    
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    public class MouseHandler implements MouseListener, MouseMotionListener{
    	
    	public boolean clicked = false;
    	public int mX;
    	public int mY;
    
    	public void mouseDragged(MouseEvent event) {
    		
    	}
    	
    	public void mouseMoved(MouseEvent event) {
    		
    	}
    
    	public void mouseClicked(MouseEvent event) {
    		event.getX();
    		event.getY();
    		System.out.println(event.getX()+ " and " + event.getY());
    		this.mX = event.getX();
    		this.mY = event.getY();
    		
    		if (mX > 0 && mY > 0){
    			clicked = true;
    		}
    	}
    	
    	public void mouseEntered(MouseEvent event) {
    		
    	}
    
    	public void mouseExited(MouseEvent event) {
    		
    	}
    
    	public void mousePressed(MouseEvent event) {
    		
    	}
    
    	public void mouseReleased(MouseEvent event) {
    		
    	}
    	
    }
    [/spoiler]
    [/spoiler]
    The second test was without these lines in game class:
    click = new MouseHandler();
    Tile.click = click;
    and the error was same nullpoint exception.

    OFFTOPIC QUESTION!
    how to i make spoilers?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Are you sure GlassTile.java is the most current? I don't see a call to update() there. Also there are compile-time errors because of undefined variables.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    should be somewhat correct now. While making the thread i had some quick ideas and prolly mixed up my different tests.

    The error code is smaller and GrassTile and Game.class is changed up to date.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Where are xOff and yOff found?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    i have not implemented it yet, all i want is to get the System.out show me the boolean value of MouseHandler
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by whijavanub
    i have not implemented it yet, all i want is to get the System.out show me the boolean value of MouseHandler
    but, you can't do that if you can't compile it.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    hmm
    Now you confused me.
    Are you referring to these?:
    public int xOff, yOff;

    If you do, then i tested, if i take them out (commented //), what happens to the program.
    Nothing. Same Error.
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by whijavanub
    hmm
    Now you confused me.
    Are you referring to these?:
    public int xOff, yOff;

    If you do, then i tested, if i take them out (commented //), what happens to the program.
    Nothing. Same Error.
    What I meant was that the GlassTiles.java you posted doesn't compile. So if you're getting error messages, it is from the last successful compile of the class.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by bullet
    What I meant was that the GlassTiles.java you posted doesn't compile. So if you're getting error messages, it is from the last successful compile of the class.
    if i take out: System.out.println(click.clicked)
    the program works
  18. #10
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by whijavanub
    if i take out: System.out.println(click.clicked)
    the program works
    What is the version of GlassTiles.java that you are using because the one you posted in the first message won't compile.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    with not compile, you mean you see error lines?
    because i dont have any problems with it, its exatcly the same i use (that is when i comment out the: System.out.println(...)
    im not sure what what u mean about version?

    But anyways i have actually discovered something new, its not what i want, but i got the error away.
    with these codes below i get answers to my console:
    false
    false
    false
    ..it goes like it should be, BUT when i click on screen, in MouseHandler Class, the boolean should turn True for a moment.
    but it still gives false.
    [if i change Tile.grass.update(); order before click = new MouseHandler(); i will get the nullpoint error message!]

    For both the tests (error message and this unaccurate MouseHandler reader) i see 2 common things what my nooby brain says: "that might be problem."

    1st.
    public static MouseHandler click;
    this line is annoying me, because i CANT take that static away whatever i try and as far i have understood the static, means that the static attributes doesn't rly want to change.
    so when when default is false, it stays false no matter what.
    At least thats what i guess might be my 1st problem.

    2nd.
    Level class
    it logicaly makes sense to me that Tile.grass is whole new GrassTile with its own MouseHandler and all,
    but maybe program doesn't understand it and goes panic when he sees a another class messing with Tile class (in that case the other class is MouseHandler class)

    Have been programming 2 months with java and mostly used tutorials logic and multiplying it on everything i can. Lately im trying to find good tutorial or somone who can help
    me to understand the arrays logic. So i could redo the tile and level classes.
    Because i have had this System.out.println problem for 2 WEEKS now and noone seems to understand the problem, far less the fix for it.
    Its getting rather annoying not to be able to see any changes at all in my program even though i have poured in lots of hours to it and annoyed lots of ppl with it.
    Thats why im here, in new forum for me, and using it as my last hope


    Anyway here are the codes for the second "almost there" test.
    Grasstile.class
    Code:
    /* Tile Grass
     * Tyypiline muru kus saab kõndida aga mingit erilist boonust ei anna.
     */
    package Tiles;
    
    import Graafika.Pilt;
    import Graafika.Screen;
    
    public class GrassTile extends Tile {
    
    	private boolean showInfo = false;
    	private int xx;
    	
    	//seda tegelt vaja ei ole. sest Tile asukoha sätib Map editor aga meh, let it be for now.
    	/*public GrassTile(int x, int y, MouseHandler click) {
    		this.x = x;
    		this.y = y;
    		this.click = click;
    	}*/
    	
    	//Tile contructor tahab nii väga callida midagi, las siis callib seda tyhja meetotit.
    	public GrassTile() {
    	//	Tile.click = click;
    	}
    
    	//siin toimuvad kõik uuendused mida Tile class teeb võimalikult kiirelt
    	public void update(){
    	//	this.click = click;
    	//	this.xx = event.getX();
    	//	System.out.println(event.getX());
    	//	System.out.println("hmm");
    		
    		Tile.clock();
    		
    		System.out.println(Tile.click.clicked);
    
    
    	/*	if (click.clicked == true) {
    			if (click.mX > (3*(x*16)) && click.mX < (3*(x*16+16)) && click.mY > (3*(y*16)) && click.mY < (3*(y*16+16))){
    				if (showInfo == false) {
    					showInfo = true;
    					click.clicked = false;
    				}else {showInfo = false; click.clicked = false;}
    			}else {showInfo = false; click.clicked = false;}
    		}	*/
    	}
    	
    	//seda ka vaja tegelt ei lähe, aga let it be for now.
    	public static void clock() {	
    
    	}
    
    	//renderib pilti
    	public void render (int x, int y, Screen screen) {
    		screen.renderTile(x<<4, y<<4, Pilt.grass);
    
    		//System.out.println(showInfo);
    		
    		//System.out.println("x: "+ (3*(x*16)) + " - " + (3*(x*16+16)) + " | y: " + (3*(y*16)) + " - " + (3*(y*16+16)));
    		/*if (showInfo == true){
    			screen.renderUnit(128-xOff/3, 0-yOff/3, Pilt.Zombie_Oleshion_Info1);
    			screen.renderUnit(176-xOff/3, 0-yOff/3, Pilt.Zombie_Oleshion_Info2);
    			screen.renderUnit(224-xOff/3, 0-yOff/3, Pilt.Zombie_Oleshion_Info3);
    			screen.renderUnit(272-xOff/3, 0-yOff/3, Pilt.Zombie_Oleshion_Info4);
    		}*/
    	}
    
    }
    Tile class
    Code:
    /*Superclass kõikide teiste Tile klasside jaoks, sisaldab igasugu trashi mis on kõikidel tileidel yhist
     * või siis mida võiks mõndadel tileidel vaja minna
     */
    package Tiles;
    
    import EpoxGame.MouseHandler;
    import Graafika.Screen;
    
    // im not sure why, aga abstract klass kõige parem
    public abstract class Tile {
    	
    	
    	/* Tile x ja y kordinaat with offset*/
    	public int x,y;
    	//public int xOff, yOff;
    	public static MouseHandler click;
    
    	
    	//kompaktne Tile informatisoon mida saab siit classist callida (for map editor)
    	public static Tile grass = new GrassTile();
    	public static Tile stone = new StoneTile();
    	public static Tile water = new WaterTile();
    	public static Tile voidtile = new voidTile();
    	
    	//@clock attributes
    	protected static int tick = 0;
    	protected static boolean ticking = false;
    	protected static long lastSec = 0;
    	
    	//igal tileil on update() method
    	public void update(){
    	//	this.click = click;
    	//	System.out.println(Tile.click.clicked);
    
    	}
    	
    	//LOOPED @clock, mis tickib 30sec
    	protected static void clock(){
    		if (tick < 30) {
    			long sec = System.currentTimeMillis() / 1000;
    			ticking = true;
    			
    			if ( sec != lastSec){
    				lastSec = sec ;
    				tick++;}
    		}else tick = 0;
    	}
    	
    	//igal tileil on render() method
    	public void render (int x, int y, Screen screen) {
    	}
    	
    	// kunagi tulevikus peaks olema see collisioni asi
    	public boolean solid(){
    		return false;
    	}
    }
    Changes in the game.class
    Code:
    private void update() {
    >>>		click = new MouseHandler();
    >>>		Tile.click = click;
    		key.update();
    		if (key.up) y--;
    		if (key.down) y++;
    		if (key.left) x--;
    		if (key.right) x++;
    		clock.update();
    		Unit_Zombie_Oleshion.update();
    		//Tile.grass.update();
    >>>		tile.grass.update();
    		//tile.update(); //not working AT ALL
    	}
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    One potential problem I see is here

    Code:
    public abstract class Tile {
    	
    	
    	/* Tile x ja y kordinaat with offset*/
    	public int x,y;
    	//public int xOff, yOff;
    	public static MouseHandler click;
    To answer your previous question, the modifier static means that that the variable click is a class variable, meaning that no matter how many instances of Tile you make, there will only be one copy of this variable in memory.

    In your Game class, I don't believe you are instantiating this variable.

    Do you have code somewhere that instantiates it?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    since i don't know what does instantiating means, i believe i have not done it.
  26. #14
  27. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by whijavanub
    since i don't know what does instantiating means, i believe i have not done it.
    Instantiating means making an instance of a class definition.

    For example, your class is called MouseHandler, so you would make an by instance by

    Code:
    MouseHandler handler = new MouseHandler();
    assuming there is a no-arg constructor in MouseHandler.java.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by bullet
    Instantiating means making an instance of a class definition.

    For example, your class is called MouseHandler, so you would make an by instance by

    Code:
    MouseHandler handler = new MouseHandler();
    assuming there is a no-arg constructor in MouseHandler.java.
    in the new test i do have it in game class, i will clean up the post 1 and make the codes less messy, so you could understand it a lil better
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo