Disegna l'immagine nel gioco Java, dando l'eccezione del puntatore null
-
28-10-2019 - |
Domanda
Sto cercando di ottenere un'immagine da caricare in un gioco su cui sto lavorando. Lo sto modellando dopo un gioco di gruppo che abbiamo costruito in classe. Nella classe del mio pannello carico un'immagine e la disegno sullo sfondo, funziona, trova la mia immagine e la disegna.
Tuttavia, non importa come carico l'immagine che sto mettendo nella mia classe di astronavi e la disegno usando un'istanza di astronavi, mi dà un'eccezione null puntatore. Non trova mai l'immagine, immagino, tuttavia l'ho inserita nella stessa posizione nella cartella di origine da cui l'immagine di sfondo viene estratta correttamente. Inoltre, se carico l'immagine dello spazio nella mia classe di giochi e la disegno sul pannello, funziona bene.
Il codice per le due classi è sotto. Ho provato a caricare l'immagine con a
spaceship = ImageIO.read(new File("Background.png"));
Così come:
Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png");
Ma ottieni lo stesso puntatore nullo in entrambi i modi. Sono perso e spero che qualcuno possa aiutare.
package temp;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameScreen extends JFrame {
//Create an instance of a spaceship object
SpaceShip spaceship;
//Create an instance of a missile object
Missile missile;
//Create an instance of a launcher object
Launcher launcher;
//create an array that holds missile objects
ArrayList<Missile> missileArray = new ArrayList<Missile>();
//Creates an array to hold spaceship objects
ArrayList<SpaceShip> enemyArray = new ArrayList<SpaceShip>();
int lives = 3;
public GameScreen() {
//sets up the panel and the parent frame
//sets the default specs of the JFrame and adds the panel to it
this.setSize(600, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//Creates a panel object and adds it to the main frame
panel panel1 = new panel(launcher, enemyArray, missileArray);
this.add(panel1);
}
}
class panel extends JPanel {
//creates a new launcher object
Launcher launcher;
//creates a spaceship object
SpaceShip space;
//creates an arraylist of spaceship objects
static ArrayList<SpaceShip> ships;
//creates an arraylist of missile objects
ArrayList<Missile> missiles;
//loads images to draw
Image backGround;
public panel(Launcher la, ArrayList<SpaceShip> ss, ArrayList<Missile> mi) {
try {
launcher = la;
ships = ss;
missiles = mi;
backGround = ImageIO.read(new File("Background.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
BufferedImage x = new BufferedImage(600, 700, BufferedImage.TYPE_INT_RGB);
Graphics g2 = x.getGraphics();
g2.drawImage(backGround, 0, 0, this);
g2.drawImage(space.img, 0, 0, this);
System.out.println(ships.size());
g.drawImage(x, 0, 0, this);
}
}
Spaceship.java
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
* @author WymoreJW
*/
class SpaceShip extends Character {
Image img = Toolkit.getDefaultToolkit().createImage("spaceShip.png");
public void SpaceShip() {
this.health = 10;
this.speed = 2;
}
}
Errore di traccia stack
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at missiledefense.panel.paintComponent(GameScreen.java:108)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered
(RepaintManager.java:1479)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run
(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters
(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Soluzione
Il problema più grande è che i tuoi commenti non si allineano al codice (semanticamente):
//creates a spaceship object
SpaceShip space;
Questo non crea un'istanza. Questo crea un riferimento a un'istanza, ma nessuna istanza. È null
. Lo passi al (mal nominato) panel
costruttore di classe e agire come se ci fosse un valore in esso. Ma non c'è.
Il NPE
, o almeno uno di loro:
g2.drawImage(space.img, 0, 0, this); // *foom*
Almeno nel codice fornito, space
non è mai inizializzato.
Consiglio anche di non incastrare tutto questo in un singolo file.
Altri suggerimenti
Le tue variabili spaziali, missili e lanciarazzi sono tutti nulli. Per istanziare una variabile è necessario chiamare il suo costruttore, ad esempio
//Actually Create an instance of a spaceship object
SpaceShip spaceship = new SpaceShip();
//Actually Create an instance of a missile object
Missile missile = new Missile();
//Actually Create an instance of a launcher object
Launcher launcher = new Launcher();
Se non lo fai, allora quando la linea panel panel1 = new panel(launcher, enemyArray, missileArray);
è eseguito il costruttore del pannello proverà a impostare launcher = la;
. Il problema arriva qui, dal momento che Los Angeles è attualmente nullo.
Consiglierei anche di rinominare la tua classe di panel a qualcosa di più descrittivo, o almeno al panel (poiché le lezioni di Java dovrebbero sempre iniziare con una lettera maiuscola).
Spero che sia di aiuto.