-1

I am programming a game , where i am working on right now is that the player rotates with no succes though. Heres the error message:

Exception in thread "Thread-2" java.lang.NullPointerException
at Schoo.NewGame.SafeIcon.getIconWidth(SafeIcon.java:29)
at Schoo.NewGame.TheRealGame.turn(TheRealGame.java:412)
at Schoo.NewGame.TheRealGame.turnPlayer(TheRealGame.java:427)
at Schoo.NewGame.TheRealGame.RepaintPlayer(TheRealGame.java:377)
at Schoo.NewGame.TheRealGame$2.run(TheRealGame.java:225)
at java.lang.Thread.run(Unknown Source)

Line 29 in SafeIcon:

return wrappee.getIconHeight();

More of it:

private Icon wrappee;
    private Icon standIn;

    public SafeIcon(Icon icon) {
        wrappee = icon;
    }

    @Override
    public int getIconHeight() {
        return wrappee.getIconHeight();
    }

    @Override
    public int getIconWidth() {
        return wrappee.getIconWidth();
    }

Line 412 in TheRealGame:

BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);

More of it:

Icon icon;
public void turn(Direction d){
    if (pt == Playere.Kaneki){
        icon = new SafeIcon(playerImage.getIcon());
        BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) bi.getGraphics();
        if (d == Direction.LEFT && p.getDirection() == Direction.UP){
            g.rotate(Math.PI / 2, playerImage.getWidth(), playerImage.getHeight());
            playerImage.getIcon().paintIcon(null, g, 0, 0);
            g.dispose();
        }
    }
    if (pt == Playere.Touka){

    }
}

This is where i found safeIcon. if you need my whole main class:

public class TheRealGame{

private static Bullets b = new Bullets();
private static boolean running = false;
private static boolean paused = false;
private static boolean right = false, left = false, up = false, down = false;
private static JFrame f;
private static ArrayList<JLabel> ae = new ArrayList<JLabel>();
private static Player p;
private static Playere pt;
private static JLabel playerImage;
private static Icon playerIcon;
private static ImageIcon imageIcon;
private static boolean attacking = false;
private static boolean info = false;
private static JLabel iy, ix, im, in, iu;
private static Enemys enemys = new Enemys();
private static ArrayList<JLabel> bullets = new ArrayList<JLabel>();

public static void main(Playere playertype){
    pt = playertype;
    p = new Player(pt);
    f = new JFrame();
    f.setVisible(true);
    f.setSize(700, 700);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.addKeyListener(new KeyListener(){
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
                up = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
                left = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
                down = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
                right = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
                if (attacking == false){
                    p.attack();
                }
            }
            if (e.getKeyCode() == KeyEvent.VK_F3){
                if (info == true){
                    info = false;
                    iy.setVisible(false);
                    ix.setVisible(false);
                    im.setVisible(false);
                    in.setVisible(false);
                    iu.setVisible(false);
                }else if (info == false){
                    info = true;
                    iy.setVisible(true);
                    ix.setVisible(true);
                    im.setVisible(true);
                    in.setVisible(true);
                    iu.setVisible(true);
                }
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
                up = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
                left = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
                down = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
                right = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
                if (attacking == true)
                    p.attack();
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }
    });
    playerImage = new JLabel();
    iy = new JLabel();
    ix = new JLabel();
    im = new JLabel();
    in = new JLabel();
    iu = new JLabel();
    iy.setLocation(0, 10);
    ix.setLocation(0, 20);
    im.setLocation(0, 30);
    in.setLocation(0, 40);
    iu.setLocation(0, 50);
    iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
    ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
    im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
    in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
    iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 300, 15);
    f.add(ix);
    f.add(iy);
    f.add(im);
    f.add(in);
    f.add(iu);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setTitle("free play - tokyo ghoul");
    Start();
    p.Paint();
}
public static void resume(){
    if (paused == false){
        return;
    }
}
public static void pause(){
    if (paused == true){
        return;
    }
}
public static void Stop(){
    if (running == false){
        return;
    }
    running = false;
}
public static void move(JLabel l, int newX, int newY){
    l.setLocation(newX, newY);
    l.repaint();
    f.repaint();
}

public static void remove(JLabel l){
    f.remove(l);
    l.setVisible(false);
}

public static JLabel getPlayerImage(){
    return playerImage;
}

public static Player getPlayer(){
    return p;
}

public static void Start(){
    running = true;

    new Thread(new Runnable(){
        @Override
        public void run() {
            int last = 0, u = 0;
            while (running == true){
                if (paused != true){
                    if (up == true){
                        p.move(p.getX(), p.getY()-1);
                        if (p.getDirection() != Direction.UP)
                            turnPlayer(Direction.UP);
                    }
                    if (down == true){
                        p.move(p.getX(), p.getY()+1);
                        if (p.getDirection() != Direction.DOWN)
                            turnPlayer(Direction.DOWN);
                    }
                    if (left == true){
                        p.move(p.getX()-1, p.getY());
                        if (p.getDirection() != Direction.LEFT)
                            turnPlayer(Direction.LEFT);
                    }
                    if (right == true){
                        p.move(p.getX()+1, p.getY());
                        if (p.getDirection() != Direction.RIGHT)
                            turnPlayer(Direction.RIGHT);
                    }
                    if (info == true){
                        int l = 10-last;
                        iy.setText("y: "+p.getY());
                        ix.setText("x: "+p.getX());
                        im.setText("enemys: "+ae.size());
                        in.setText("next enemy: "+l);
                        iu.setText("Updated "+u+" times.");
                        RefreshInfo();
                    }
                    if (p.getY() >= +334){
                        p.move(p.getX(), +334);
                    }
                    if (p.getY() <= -334){
                        p.move(p.getX(), -334);
                    }
                    if (p.getX() >= 675){
                        p.move(675, p.getY());
                    }
                    if (p.getX() <= -12){
                        p.move(-12, p.getY());
                    }
                    RepaintAllEnemys();
                    Enemy.UpdateAll();
                    playerImage.repaint();
                    RepaintPlayer();
                    enemys.Update();
                    b.Update();
                    f.repaint();
                    if (info != true){
                        f.repaint();
                    }
                    if (last == 10){
                        Random r = new Random();
                        int x = 1+r.nextInt(2), y = 1+r.nextInt(2), distance = 1+r.nextInt(570), nx = 0, ny = 0;
                        if (x == 1){

                        }
                        last = 0;
                    }
                    last++;
                    u++;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public static void RefreshInfo(){
    f.remove(iy);
    f.remove(ix);
    f.remove(im);
    f.remove(in);
    f.remove(iu);
    f.add(ix);
    f.add(iy);
    f.add(im);
    f.add(in);
    f.add(iu);
    iy.setLocation(0, 10);
    ix.setLocation(0, 20);
    im.setLocation(0, 30);
    in.setLocation(0, 40);
    iu.setLocation(0, 50);
    iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 200, 15);
    iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
    ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
    im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
    in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
    f.repaint();
}

public static void UpdateAll(){

}

public static void Paint(JLabel imgs, int x, int y, Icon file){
    Icon img = file;
    imgs.setIcon(img);
    imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
    imgs.setLocation(x, y);
    imgs.setVisible(true);
    f.add(imgs);
    imgs.setVisible(true);
}

public static void addBullet(JLabel l, int x, int y, Icon icon){
    Paint(l, x, y, icon);
    bullets.add(l);
}

public static void moveBullet(int newX, int newY, JLabel label){
    if (bullets.contains(label)){
        label.setLocation(newX, newY);
        label.repaint();
        f.repaint();
        label.setVisible(true);
    }
}

public static void Repaint(JLabel l){
    f.remove(l);
    l.setBounds((int)l.getLocation().getX(), (int)l.getLocation().getY(), l.getWidth(), l.getHeight());
    f.add(l);
}

public static void addAE(JLabel l){
    ae.add(l);
}

public static void RepaintAllEnemys(){
    for (int i = 0; i < ae.size(); i++){
        ae.get(i).repaint();
    }
}

public static void MovePlayer(int x, int y){
    playerImage.setLocation(x, y);
    playerImage.repaint();
    f.repaint();
}

public static void paint(JLabel l, int Y, int X){
    l.setLocation(X, Y);
    l.setBounds(X, Y, l.getIcon().getIconWidth(), l.getIcon().getIconHeight());
    l.setVisible(true);
    f.add(l);
    l.setVisible(true);
    l.repaint();
    f.repaint();
}

public static void RepaintPlayer(){
    if (p.isAttacking() == true){
        if (pt == Playere.Kaneki){
            if (attacking == false){
                ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
                playerImage.setIcon(img);
                playerIcon = img;
                attacking = true;
                imageIcon = img;
            }
        }
        if (pt == Playere.Touka){
            if (attacking == false){
                ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_shoot.png"));
                playerImage.setIcon(img);
                playerIcon = img;
                attacking = true;
                imageIcon = img;
            }
        }
    }else{
        if (pt == Playere.Kaneki){
            if (attacking == true){
                ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
                playerImage.setIcon(img);
                playerIcon = img;
                attacking = false;
                imageIcon = img;
            }
        }
        if (pt == Playere.Touka){
            if (attacking == true){
                ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
                playerImage.setIcon(img);
                playerIcon = img;
                attacking = false;
                imageIcon = img;
            }
        }
    }
    turnPlayer(p.getDirection());
    MovePlayer(p.getX(), p.getY());
    playerImage.repaint();
    f.repaint();
}

public static void paintplayer(){
    if (pt == Playere.Kaneki){
        ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png"));
        playerImage.setIcon(imgs);
        playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
        playerImage.setLocation(274, 277);
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
        imageIcon = imgs;
    }
    if (pt == Playere.Touka){
        playerImage.setIcon(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")));
        playerImage.setBounds(p.getX(), p.getY(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconWidth(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconHeight());
        playerImage.setLocation(274, 277);
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
        playerIcon = playerImage.getIcon();
    }
}

public static Bullets getBullets(){
    return b;
}
Icon icon;
public void turn(Direction d){
    if (pt == Playere.Kaneki){
        icon = new SafeIcon(playerImage.getIcon());
        BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) bi.getGraphics();
        if (d == Direction.LEFT && p.getDirection() == Direction.UP){
            g.rotate(Math.PI / 2, playerImage.getWidth(), playerImage.getHeight());
            playerImage.getIcon().paintIcon(null, g, 0, 0);
            g.dispose();
        }
    }
    if (pt == Playere.Touka){

    }
}

public static void turnPlayer(Direction d){
    try {
        TheRealGame.class.newInstance().turn(d);
    } catch (InstantiationException e) {
        System.out.println("Something went wrong!");
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        System.out.println("Something went wrong!");
        e.printStackTrace();
    }
}

public enum Direction{
    LEFT, UP, RIGHT, DOWN;
}
}
Community
  • 1
  • 1
Rof
  • 31
  • 6
  • 1
    Too much unrelated code, and the link for SafeIcon seems to be wrong (and why do you need that in the first place, what does it provide over ImageIcon etc?). The place you *should show* for sure, is how you create the icon. – Durandal Dec 08 '15 at 19:41
  • I want to make the Icon to an BufferedImage – Rof Dec 08 '15 at 19:44
  • Stiil, the point of SafeIcon is what? From what you show its just a wrapper (around something that *most likely* does not exist in your case). The NullPointerException means there is a reference in there that is null and attempted to be dereferenced. Which means the reason is most likely hidden somewhere else, where you either forgot to initialize something or something in the initialization went wrong (and you ended up passing null to the constructor of the icon). From what I see in your turn(d) method, you don't need SafeIcon, any Icon would do. – Durandal Dec 08 '15 at 19:52
  • Ok, but do you know how i could make the Icon to a BufferedIcon is there a way? – Rof Dec 08 '15 at 19:54
  • Your problem is that you don't *have* an icon to paint. Its NULL, simple as that. Debug to see *why* PlayerImage.getIcon() returns NULL! – Durandal Dec 08 '15 at 20:00
  • But i have set `playerImage` it should not return null! – Rof Dec 08 '15 at 20:03
  • @andrucz i know what it is but it should not be null. – Rof Dec 08 '15 at 20:05

1 Answers1

0

That's because when you are creating a SafeIcon instance in icon = new SafeIcon(playerImage.getIcon()); you are defining null to wrapee variable.

If you need wrapee to be a non null value, adjust constructor to be more safe, so it will be much more easy to detect root cause of NPEs:

public SafeIcon(Icon icon) {
    if (icon == null) {
        throw new NullPointerException("icon"); // or IllegalArgumentException
    }
    this.wrapee = icon;
}

Then check if playerImage JLabel instance should always have an icon defined. If so, define an icon to it and your problem will be solved.

andrucz
  • 1,971
  • 2
  • 18
  • 30
  • But them it will still not work because for some reason the "icon" variable is null – Rof Dec 08 '15 at 19:56
  • Adjust in constructor just increase code "safety". It will be more easy do detect problems. I've just edited my answer. – andrucz Dec 08 '15 at 20:00
  • But i have set an Icon when starting the game – Rof Dec 08 '15 at 20:02
  • No, you haven't. Debug and see why icon are not being defined in RepaintPlayer method. Might be because p.isAttacking() returns false. – andrucz Dec 08 '15 at 20:06
  • Can't find it, when i go in debugger mode or i debug myself – Rof Dec 08 '15 at 20:08
  • Might be because p.isAttacking() returns false, then setIcon is not called. – andrucz Dec 08 '15 at 20:08
  • It does get calld in the `else` statement it gets called after setting the ImageIcon – Rof Dec 08 '15 at 20:10
  • So might be because pt is not Playere.Kaneki nor Playere.Touka or attacking is false. Pretty sure setIcon is not being called. – andrucz Dec 08 '15 at 20:13
  • In Playere you only get Kaneki and Touka it is deffently called and when not it is called when starting that game. – Rof Dec 08 '15 at 20:16