简体   繁体   中英

Java NullPointerException on Applet

I am trying to embed an applet into an HTML for demonstration in my Uni class. Every time I try to run the execute the applet, I get a NullPointerException dialog. What am I doing wrong?

<html>
  <head>
    <title> This is my Pacman! </title>
      <body bgcolor = black text= yellow>
        <center>
          <applet code="pacman.InitialClass.class" archive="pacSimq.jar"
            width=300 height=350>
          </applet>
         </center>
         <hr>
           <a href="https://github.com/awernick/PacSim"> The repository. </a>
      </body>
</html>

This is the main class for the applet. The jar contains multiple classes also.

package pacman;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.Random;

@SuppressWarnings("serial")
public  class InitialClass extends Applet implements Runnable, KeyListener {

    private final int WIDTH = 224;
    private final int HEIGHT = 288;
    private Map map;
    private Hero pacman;
    private Blinky blinky;
    private Graphics second;
    private Image image, bg, current,current_bl, pm1, pm2, pm3, pm4, pm5, pm6, pm7, pm8, pm9,bl1,bl2,bl3,bl4,bl5,bl6,bl7,bl8;
    private URL base;
    private int counter;

    public void init() {
        setSize(WIDTH, HEIGHT);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Pac-Man");
        frame.setResizable(false);
        try {
            base = getDocumentBase();
            System.out.println(base);
        } catch (Exception e) {

        }

        pm1 = getImage(base, "data/pac_1.png");
        pm2 = getImage(base, "data/pac_2.png");
        pm3 = getImage(base, "data/pac_3.png");
        pm4 = getImage(base, "data/pac_4.png");
        pm5 = getImage(base, "data/pac_5.png");
        pm6 = getImage(base, "data/pac_6.png");
        pm7 = getImage(base, "data/pac_7.png");
        pm8 = getImage(base, "data/pac_8.png");
        pm9 = getImage(base, "data/pac_9.png");

        bl1 = getImage(base, "data/bl_1.png");
        bl2 = getImage(base, "data/bl_2.png");
        bl3 = getImage(base, "data/bl_3.png");
        bl4 = getImage(base, "data/bl_4.png");
        bl5 = getImage(base, "data/bl_5.png");
        bl6 = getImage(base, "data/bl_6.png");
        bl7 = getImage(base, "data/bl_7.png");
        bl8 = getImage(base, "data/bl_8.png");

        bg = getImage(base, "data/bg1.png");

        current = pm1;
        current_bl = bl1;
    }

    @Override
    public void start() {
        map = new Map();
        pacman = new Hero();
        blinky = new Blinky(map,pacman);

        Thread graphics_thread = new Thread(this);
        Thread blinky_thread = new Thread(blinky);
        Thread pacman_thread = new Thread(pacman);
        graphics_thread.start();
        blinky_thread.start();
        pacman_thread.start();
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    @Override
    public void run() {
        while (true) {
            //pacman.update();
            //blinky.update(pacman.getCenterX(), pacman.getCenterY());
            if (pacman.isMvRight()) {
                current = pm2;
                current_bl = bl2;
            } else if (pacman.isMvLeft()) {
                current = pm4;
                current_bl = bl4;
            } else if (pacman.isMvUp()) {
                current = pm6;
                current_bl = bl6;
            } else if (pacman.isMvDown()) {
                current = pm8;
                current_bl = bl8;
            }
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0,this);
        g.drawImage(current, pacman.getCenterX(), pacman.getCenterY(), this);
        g.drawImage(current_bl, blinky.getCenterX(), blinky.getCenterY(), this);
    }

    @Override
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            pacman.setMvUp(true);
            //System.out.println("hello");
            pacman.moveUp();
            //System.out.println("hello");

            break;

        case KeyEvent.VK_DOWN:
            //System.out.println("hello");
            pacman.setMvDown(true);
            pacman.moveDown();
            break;

        case KeyEvent.VK_LEFT:
            pacman.setMvLeft(true);
            pacman.moveLeft();
            break;

        case KeyEvent.VK_RIGHT:
            pacman.setMvRight(true);
            pacman.moveRight();
            break;

        case KeyEvent.VK_SPACE:
            pacman.stop();
            break;

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

This is the error the Java Console is showing me.

java.lang.NullPointerException
    at pacman.InitialClass.init(InitialClass.java:34)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I packed it into a jar through eclipse, yet I still get NullPointerException. What might be causing it?

    Frame frame = (Frame) this.getParent().getParent();
    frame.setTitle("Pac-Man");  // line 34

The only thing that might be null on line 34 is the frame attribute. At this point in the applet life cycle, the applet is apparently yet to be added to a root frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM