0

I'm trying to make a fairly simple game, there is a player class that when instantiated an exception when trying to be drawn. The below code is the player class. Its whats being called to create the player object in the main game class.

package space.invaders;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
  *
* @author Your Name
* <Tristan, Teacher: Anthony , Last Edited: , Date Created, Program Description>
*/
public class player extends JPanel{
public static int px,py;   
public static int rx = 1, ry = 1, width =75, height = 50;
public Image player;
static Rectangle shot = new Rectangle(rx,ry,width,height);
static Graphics g;
String imagePath;
URL playerPath;



// draws the palyer    
public void paintPlayer(){
    playerPath = getClass().getResource("/graphics/player.png");
    try{
        player = ImageIO.read(playerPath);
    }catch(IOException io){
        System.out.print("The player picture didn't load properly");
    }
    g.drawImage(player, px, py, player.this);
}
// draws the shot
static void paintShot(){
    g.drawRect((int) shot.getX(), (int) shot.getY(),(int)shot.getWidth(),(int)shot.getHeight());
}
// moves the shot upwards, though might be unnececcary
static int moveShot (){
    for(ry = (int) shot.getY(); ry < 300; ry++){
        return ry;
    }
    return ry;
}

int setX(int x){
    return px;
}

int setY(int y){
    return py;
}

// I think this can be void since your * reminder to test that *
String setImage(String imagePath){        
    return imagePath;
}



// player movement control
public static class movement implements KeyListener {

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    rx += 5;

    }
    else if(e.getKeyCode() == KeyEvent.VK_LEFT){
    rx -= 5;

    }
    else if(e.getKeyCode() == KeyEvent.VK_UP){
    ry -= 5;

    }
    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    ry += 5;
    }
        else if(e.getKeyCode() == KeyEvent.VK_SHIFT){
            paintShot();
            moveShot();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    }
}`

This is the main game class which is calling and creating the invader and player classes(same problem with invader as exists with player)

package space.invaders;

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import space.invaders.player.movement;

/**
 *
 * @author Your Name
 * <Tristan , Teacher: Anthony , Last Edited: , Date Created, Program Description>
 */
public class mainGame extends JFrame{
    public player p1 = new player();
    public mainGame(){        
        EventQueue.invokeLater(new Runnable(){
            @Override
            public void run() {
                createGUI();
                startGame();
                drawInvaders(1);

            }            
        });
    }
    void createGUI(){
        JFrame gameWindow = new JFrame("Space Invaders");
        JPanel observableWindow = new JPanel();
        gameWindow.add(observableWindow);
        observableWindow.setBackground(Color.black);
        gameWindow.setVisible(true);
        gameWindow.setSize(425,300);
        gameWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        gameWindow.addKeyListener(new movement());
    }
    void startGame(){

        p1.setImage("/graphics/player.png");
        p1.setX(50);
        p1.setY(50);
        p1.paintPlayer();
    }
    void drawInvaders(int numInvaders){
        for(int i = 0; i < numInvaders; i++){
        int randX = (int) (Math.random() * this.getWidth());
        int randY = (int) (Math.random() * this.getHeight());
        invader a = new invader();
        a.setX(randX);
        a.setY(randY);
        a.newInvader();
        }
    }
}

Here is the error code

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at space.invaders.player.paintPlayer(player.java:31)
    at space.invaders.mainGame.startGame(mainGame.java:48)
    at space.invaders.mainGame$1.run(mainGame.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 12 seconds)
Tristan
  • 31
  • 7
  • If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. – Hovercraft Full Of Eels Nov 17 '15 at 16:14
  • But here you also appear to be doing Graphics all wrong. Never create a Graphics field as you're doing here, `g` as it's bound to be null or ineffective at some point. Instead draw in a JPanel's paintComponent method as the Swing tutorials will show you. – Hovercraft Full Of Eels Nov 17 '15 at 16:15

0 Answers0