Klassen Override Problem

tom2208

Mitglied
Hallo,
Ich habe folgendes Problem:
Ich erhalte eine NullPointerException und brauche etwas Hilfe:
Ich habe eine abstrakte Klasse die ein Interface implementiert nun ist im Interface
" public Loction getLoction(); " (Interfacename: RenderObject)
In der abstrakten Klasse: (Klassenname: Block)
" @Override
public Loction getLoction(){return loc} "

Jetzt habe ich eine weitere Klasse BlockStone. Dann erzeuge ich einen neuen BlockStone und füge ihn in eine ArrayList<RenderObject> ro.
Ich bekomme nun null bei ro.get(i).getLoction() zurück. In BlockStone und Block sind jeweils getLoction überschreiben, liegt darin der Fehler?
Wenn ja wäre es nett wenn du mir einen Alternativen weg zeigen könntest wie ich trotzdem das gewünschte Ziel erreiche.

LG Tom
 
Hier etwas Code:
Block Klasse:
Java:
public abstract class Block implements RenderObject{
   
    Location loc;
    BufferedImage img;
   
    public Block(Location loc, BufferedImage img){
        this.loc = loc;
        this.img = img;
    }
   
    @Override
    public void draw(Graphics g){
        g.drawImage(img, Tile.getDisplayXPos(loc.getX()), Tile.getDisplayYPos(loc.getY()), null);
    }
   
    public void update() {
    }
   
    public HitBox getHitBox() {
        return null;
    }
   
    @Override
    public Location getLocation() {
        return loc;
    }

}

StoneBlock Klasse:
Java:
public class BlockStone extends Block{

    private Location loc;
    private HitBox hitbox;
   
    public BlockStone(Location loc){
       
        super(loc, Textures.block_stone);
       
    }

    @Override
    public void update() {
       
    }

    @Override
    public HitBox getHitBox() {
        return hitbox;
    }

    @Override
    public Location getLocation() {
        return loc;
    }
   
}

Interface:
Java:
public interface RenderObject {
   
    public Location getLocation();
   
    public void draw(Graphics g);
   
}

Und die Welt klasse:

Java:
package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import Blocks.Block;
import Blocks.BlockStone;
import Entity.EntityPlayer;
import NewUniverseMain.Keyboard;
import NewUniverseMain.Textures;
import NewUniverseMain.main;
import Tiles.Tile;
import Tiles.TileGrass;

public class World {
   
    public static float X = (int) (main.width * 0.5); // 1920 / 2
    public static float Y = (int) (main.height * 0.5); // 1080 / 2
    public float xpos;
    public float ypos;
   
    private int[] keyfield = {-1,-1};
   
    private Tile[][] tiles; // Tiles of the world
   
    private BufferedImage[] img = {Textures.player};
    private EntityPlayer player;
    private ArrayList<RenderObject> ro = new ArrayList<>();
    private Block testblock = new BlockStone(new Location(3.5F, 3.5F));
   
    /**
     * This class is a combination of a camera and the game world
     *
     * @param tiles
     * @param startxpos
     * @param startypos
     */
    public World(Tile[][] tiles, ArrayList<RenderObject> ro, float startxpos, float startypos){
       
        //this.tiles = tiles;
        //this.ro = ro;
       
        xpos = startxpos;
        ypos = startypos;
       
        Tile[][] tiles00 = new Tile[main.WORLD_SIZE][main.WORLD_SIZE];
       
        for(int x = 0; x < main.WORLD_SIZE; x++){
            for(int y = 0; y < main.WORLD_SIZE; y++){
                tiles00[x][y] = new TileGrass();
            }
        }
       
        this.ro.add(new BlockStone(new Location(3.5F, 3.5F)));
       
        this.tiles = tiles00;
        Location loc = new Location(xpos, ypos);
        HitBox hitbox = new HitBox(loc, img[0].getWidth() / Tile.TILE_SIZE, img[0].getHeight() / Tile.TILE_SIZE, 10, true);
        player = new EntityPlayer(img, 1, 1, loc, hitbox);
        this.ro.add(player);
       
    }
   
    public void draw(Graphics g){

        //drawing world
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, main.width, main.height);
       
        int ix = 0;
        int iy = 0;
       
        //draw tiles
        for(int x = 0; x < main.WORLD_SIZE; x++){
            ix++;
            for(int y = 0; y < main.WORLD_SIZE; y++){
                iy++;
                tiles[x][y].draw(g, (int)(X - xpos * Tile.TILE_SIZE + ix * Tile.TILE_SIZE), (int)(Y - ypos * Tile.TILE_SIZE + iy * Tile.TILE_SIZE));
            }
            iy = 0;
        }
       
        //draw renderObjects
        RenderObject[] objects = new RenderObject[ro.size()];
        ArrayList<RenderObject> ro2 = ro;
       
        int counter = 0;
        for(int i2 = 1; i2 < ro.size(); i2++){
           
            int small = 0;

            for(int i = 1; i < ro2.size(); i++){ // evt ro.get().getloc != player.getLoc muss überprüft werden
                System.out.println(small + ": " + ro2.get(small).getLocation() + " " + player.getLocation().getX());
                System.out.println(testblock.getLocation());
                if(ro2.get(small).getLocation().getY() > ro2.get(i).getLocation().getY()){
                    small = i;
                }
           
            }
            objects[counter] = ro2.get(small);
            ro2.remove(small);
            counter++;
               
        }
       
        for(int i = 0; i < objects.length; i++){
            if(objects[i] != null) objects[i].draw(g);
        }



    }
   
    public void update(float tslf){
       
        /**
         *   0   W
         *  3#1 A#D
         *   2   S
         */
       
        /**
        *    PLAYER CONTROL (KEYBOARD)
        */
        if(Keyboard.isKeyPressed(KeyEvent.VK_W)){
            if(keyfield[0] != 0 && keyfield[1] != 0){   
                keyfield[1] = keyfield[0];
                keyfield[0] = 0;
            }
        }
        if(Keyboard.isKeyPressed(KeyEvent.VK_S)){
            if(keyfield[0] != 2 && keyfield[1] != 2){   
                keyfield[1] = keyfield[0];
                keyfield[0] = 2;
   
            }
        }
        if(Keyboard.isKeyPressed(KeyEvent.VK_A)){
            if(keyfield[0] != 3 && keyfield[1] != 3){   
                keyfield[1] = keyfield[0];
                keyfield[0] = 3;
            }
        }
        if(Keyboard.isKeyPressed(KeyEvent.VK_D)){
            if(keyfield[0] != 1 && keyfield[1] != 1){   
                keyfield[1] = keyfield[0];
                keyfield[0] = 1;
            }
        }
       
        if(!Keyboard.isKeyPressed(KeyEvent.VK_W)){
            if(keyfield[0] == 0) keyfield[0] = -1;
            if(keyfield[1] == 0) keyfield[1] = -1;
        }
        if(!Keyboard.isKeyPressed(KeyEvent.VK_D)){
            if(keyfield[0] == 1) keyfield[0] = -1;
            if(keyfield[1] == 1) keyfield[1] = -1;
        }
        if(!Keyboard.isKeyPressed(KeyEvent.VK_S)){
            if(keyfield[0] == 2) keyfield[0] = -1;
            if(keyfield[1] == 2) keyfield[1] = -1;
        }
        if(!Keyboard.isKeyPressed(KeyEvent.VK_A)){
            if(keyfield[0] == 3) keyfield[0] = -1;
            if(keyfield[1] == 3) keyfield[1] = -1;
        }
       
        if(keyfield[0] == -1){
            keyfield[0] = keyfield[1];
            keyfield[1] = -1;
        }
       
        //update blocks
        for(int i = 0; i < ro.size(); i++){
           
        }
       
        //updating world
        player.move(keyfield[0], tslf);
        player.update();
       
    }
   
    public void move(int x, int y){
        X += x;
        Y += y;
    }
   
}
 
Du solltest nicht ein weiters Attribut "loc" in der BlockStone-Klasse haben. Das Attribut loc von der Block-Klasse wird mitvererbt. (Dementsprechend ist das "loc" von StoneBlock null.)
 

Neue Themen


Zurück
Oben