IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
Blickr Goodfray:15.5.2016 15:53

Exception in thread "Timer-0" java.lang.NullPo­interException
at hra.Okno$Sche­duleTask.run(Ok­no.java:126)
at java.util.Timer­Thread.mainLo­op(Timer.java:555)
at java.util.Timer­Thread.run(Ti­mer.java:505)

package hra;


import javax.swing.ImageIcon;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Gulka extends Obraz implements InterKsicht {

    private int smerX;
    private int smerY;

    public Gulka() {
        this.smerX = 1;
        this.smerY = -1;

        ImageIcon ii = new ImageIcon("nemo.gif");
        obrazok = ii.getImage();

        sirkaObrazka = obrazok.getWidth(null);
        vyskaObrazka = obrazok.getHeight(null);

        resetGulky();

    }

    public void pohybGulky() {
        x = x + smerX;
        y = y + smerY;

        if (x == 0) {
            nastavSmerX(1);

        }

        if (x == SIRKA - sirkaObrazka) {
            nastavSmerX(-1);

        }

        if (y == 0) {
            nastavSmerY(1);

        }
    }

    private void resetGulky() {
        x = START_GULKA_X;
        y = START_GULKA_Y;

    }

    public void nastavSmerX(int x) {
        this.smerX = x;
    }

    public void nastavSmerY(int y) {
        this.smerY = y;
    }
    public int dajSmerY() {
        return smerY;
    }
package hra;


import java.awt.EventQueue;
import javax.swing.JFrame;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Hra extends JFrame {
    public Hra() {
        nastavGUI();
    }

    private void nastavGUI() {
        add(new Okno());
        setTitle("Hra 1000!");

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(InterKsicht.SIRKA, InterKsicht.VYSKA);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Okno hra = new Okno();
            hra.setVisible(true);
        });
                }
}
package hra;


import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Hrac extends Obraz implements InterKsicht {

    private int smerX;

    public Hrac() {

        ImageIcon ii = new ImageIcon("Postava.png");
        obrazok = ii.getImage();

        sirkaObrazka = obrazok.getWidth(null);
        vyskaObrazka = obrazok.getHeight(null);

        resetHraca();
    }

    public void pohybHraca() {
        x = x + smerX;

        if (x <= 0) {
            x = 0;
        }

        if (x >= SIRKA - sirkaObrazka) {
            x = SIRKA - sirkaObrazka;
        }
    }

    public void stlaceneTlacitko(KeyEvent e) {
        int key = e.getKeyCode();

        if(key == KeyEvent.VK_LEFT) {
            smerX = -1;
        }
        if(key == KeyEvent.VK_RIGHT) {
            smerX = 1;
        }
    }
    public void pusteneTlacitko(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            smerX = 0;

        }
        if (key == KeyEvent.VK_RIGHT) {
            smerX = 0;

        }

    }
    private void resetHraca() {
        x = START_HRAC_X;
        y = START_HRAC_Y;
    }
}
package hra;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public interface InterKsicht {

    public static final int SIRKA = 300;
    public static final int VYSKA = 400;
    public static final int DOLNY_OKRAJ = 390;
    public static final int N_TEHAL = 30;
    public static final int START_HRAC_X = 200;
    public static final int START_HRAC_Y = 360;
    public static final int START_GULKA_X = 230;
    public static final int START_GULKA_Y = 355;
    public static final int DELAY = 1000;
    public static final int INTERVAL = 10;

}
package hra;


import java.awt.Image;
import java.awt.Rectangle;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Obraz {
    protected int x;
    protected int y;
    protected int sirkaObrazka;
    protected int vyskaObrazka;
    protected Image obrazok;


    public void nastavX(int x) {
        this.x = x;
    }

    public int dajX() {
        return x;
    }

    public void nastavY(int y) {
        this.y = y;
    }

    public int dajY() {
        return y;
    }

    public int dajSirkuObrazka() {
        return sirkaObrazka;
    }

    public int dajVyskuObrazka() {
        return vyskaObrazka;
    }

    Image dajObrazok() {
        return obrazok;
    }

    Rectangle dajObdlznik() {
        return new Rectangle(x, y, obrazok.getWidth(null), obrazok.getHeight(null));
    }

}
package hra;


import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Okno extends JPanel implements InterKsicht {

    private Timer casovac;
    private String sprava = "Game Over";
    private Gulka gulka;
    private Hrac hrac;
    private Tehla tehly[];
    private boolean hra = true;


    public Okno() {
        nastavenieOkna();
    }
    private void nastavenieOkna() {
        addKeyListener(new Adapter());
        setFocusable(true);

        tehly = new Tehla[N_TEHAL];
        setDoubleBuffered(true);
        casovac = new Timer();
        casovac.scheduleAtFixedRate(new ScheduleTask(), DELAY, INTERVAL);
    }


    public void addNotify() {
        super.addNotify();
        nastavenieHry();
    }

    private void nastavenieHry() {
        gulka = new Gulka();
        hrac = new Hrac();

        int k = 0;

        for (int i = 0; i < 5; i++ ) {
            for (int j = 0; j < 6; j++) {
                tehly[k] = new Tehla(j*40+30,i*10+50);
                k++;
            }
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        if(hra) {
            kresliObjekt(g2d);
        }else {
            hraSkoncila(g2d);
        }
        Toolkit.getDefaultToolkit().sync();

    }

    private void kresliObjekt(Graphics2D g2d) {
        g2d.drawImage(gulka.dajObrazok(), gulka.dajX(), gulka.dajY(),
                gulka.dajSirkuObrazka(),gulka.dajVyskuObrazka(),this);

        g2d.drawImage(hrac.dajObrazok(), hrac.dajX(),hrac.dajY(),
                hrac.dajSirkuObrazka(),hrac.dajVyskuObrazka(), this);

        for (int i = 0; i < N_TEHAL; i++) {
            if (!tehly[i].znicenaTehla()) {
                g2d.drawImage(tehly[i].dajObrazok(),tehly[i].dajX(),tehly[i].dajY(),
                        tehly[i].dajSirkuObrazka(),tehly[i].dajVyskuObrazka(), this);
            }
        }
    }

   private void hraSkoncila(Graphics2D g2d) {
       Font font = new Font ("Verdana", Font.BOLD, 18);
       FontMetrics metr = this.getFontMetrics(font);

       g2d.setColor(Color.BLACK);
       g2d.setFont(font);
       g2d.drawString(sprava,(InterKsicht.SIRKA - metr.stringWidth(sprava)) / 2,InterKsicht.SIRKA / 2);
   }

   private class Adapter extends KeyAdapter {

        public void pusteneTlacitko(KeyEvent e) {
            hrac.pusteneTlacitko(e);
        }

        public void stlaceneTlacitko(KeyEvent e) {
            hrac.stlaceneTlacitko(e);
        }
    }
   private class ScheduleTask extends TimerTask {

       public void run() {
           gulka.pohybGulky();
           hrac.pohybHraca();
           kolizie();
           repaint();
       }
   }
   private void koniecHry() {
       hra = false;
       casovac.cancel();
   }

   private void kolizie() {

       if (gulka.dajObdlznik().getMaxY() > InterKsicht.DOLNY_OKRAJ) {
           koniecHry();
       }

       for (int i = 0, j = 0; i < N_TEHAL; i++) {

           if (tehly[i].znicenaTehla()) {
               j++;
            }
           if (j == N_TEHAL) {
               sprava = "VICTORY";
               koniecHry();
           }
       }

       if ((gulka.dajObdlznik().intersects(hrac.dajObdlznik()))) {
           int poziciaHraca = (int) hrac.dajObdlznik().getMinX();
           int poziciaGulky = (int) hrac.dajObdlznik().getMinX();

           int prvy = poziciaHraca + 8;
           int druhy = poziciaHraca + 16;
           int treti = poziciaHraca + 24;
           int stvrty = poziciaHraca + 32;

           if(poziciaGulky < prvy) {
               gulka.nastavSmerX(-1);
               gulka.nastavSmerY(-1);
           }

           if(poziciaGulky >= prvy && poziciaGulky < druhy) {
               gulka.nastavSmerX(-1);
               gulka.nastavSmerY(-1 * gulka.dajSmerY());

           }

           if (poziciaGulky >= druhy && poziciaGulky < treti) {
               gulka.nastavSmerX(0);
               gulka.nastavSmerY(-1);
           }

           if (poziciaGulky >= treti && poziciaGulky < stvrty) {
               gulka.nastavSmerX(1);
               gulka.nastavSmerY(-1 * gulka.dajSmerY());

           }

           if (poziciaGulky > stvrty) {
               gulka.nastavSmerX(1);
               gulka.nastavSmerY(-1);

           }
        }

       for (int i = 0; i < N_TEHAL; i++) {

           if ((gulka.dajObdlznik().intersects(tehly[i].dajObdlznik()))) {

               int lavaGulka = (int) gulka.dajObdlznik().getMinX();
               int vyskaGulky = (int) gulka.dajObdlznik().getHeight();
               int sirkaGulky = (int) gulka.dajObdlznik().getWidth();
               int vrchGulky = (int)  gulka.dajObdlznik().getMinY();

               Point pravyBod = new Point(lavaGulka + sirkaGulky + 1, vrchGulky);
               Point lavyBod = new Point(lavaGulka - 1,vrchGulky);
               Point vrchnyBod = new Point(lavaGulka, vrchGulky - 1);
               Point spodnyBod = new Point(lavaGulka, vrchGulky + vyskaGulky + 1);

               if (!tehly[i].znicenaTehla()) {
                   if (tehly[i].dajObdlznik().contains(pravyBod)) {
                       gulka.nastavSmerX(-1);
                   }else if (tehly[i].dajObdlznik().contains(lavyBod)) {
                       gulka.nastavX(1);
                   }

                   if (tehly[i].dajObdlznik().contains(vrchnyBod)) {
                       gulka.nastavY(1);
                   }else if (tehly[i].dajObdlznik().contains(spodnyBod)) {
                       gulka.nastavSmerY(-1);

                   }
                   tehly[i].nastavZnicenu(true);

               }
           }
       }
   }



}
package hra;


import javax.swing.ImageIcon;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author david
 */
public class Tehla extends Obraz {
    private boolean znicena;


    public Tehla(int x, int y) {
        this.x = x;
        this.y = y;

        ImageIcon ii = new ImageIcon("Tehla.png");
        obrazok = ii.getImage();

        sirkaObrazka = obrazok.getWidth(null);
        vyskaObrazka = obrazok.getHeight(null);

        znicena = false;
    }

    public boolean znicenaTehla() {
        return znicena;
    }

    public void nastavZnicenu(boolean a) {
        this.znicena = a;
    }
}
 
Odpovědět
15.5.2016 15:53
Avatar
B42P6
Člen
Avatar
Odpovídá na Blickr Goodfray
B42P6:15.5.2016 18:56

Problém je v tom že atributy

private Gulka gulka ;

a

private Hrac hrac;

nie sú initializované atributy (majú hodnotu null). Ako tak pozerám inicializuješ ich v metode nastavenieHry(), ale túto metódu nikdy nezavoláš. Neskôr voláš metódy na týchto null objektoch v triede ScheduleTask:

public void run() {
           gulka.pohybGulky(); //gulka má hodnotu null, hodi to NullPointerException
           hrac.pohybHraca(); //hrac ma tiez hodnotu null, takze to tiez hodi NPE
           kolizie();
           repaint();
       }

Dúfam že na riešenie prídeš aj sám.

BTW: Nabudúce si skús sám zistiť čo znamená ten a ten error.

Editováno 15.5.2016 18:56
Nahoru Odpovědět
15.5.2016 18:56
'long long long' is too long for GCC
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.