什么是Suspended exception NullPointerException(post中的完整错误)以及如何解决它

时间:2015-05-10 19:33:07

标签: java eclipse

我没有使用任何外部库,我正在尝试解决以下错误:

  

游戏(4)[Java应用]
      com.pipelinegaming.pixelproblem.Game at localhost:50673
          线程[AWT-EventQueue-0](运行)
          线程[显示](暂停(异常NullPointerException))
              Sprite.load()行:25
              Sprite。(int,int,int,SpriteSheet)行:18
              精灵。()行:9
              Screen.render()行:40
              Game.render()行:137
              Game.run()行:97
              Thread.run()行:745
          线程[DestroyJavaVM](运行)

      /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java
(2015年5月10日,下午3:11:03)

游戏类:

package com.pipelinegaming.pixelproblem;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.pipelinegaming.pixelproblem.graphics.Screen;
import com.pipelinegaming.pixelproblem.input.Keyboard;

@SuppressWarnings("unused")
public class Game extends Canvas implements Runnable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static int width = 300;
    public static int height = width /16 * 9;
    public static int scale = 3;
    public static String title = "Pixel Problem";
    private Keyboard key;
    private Screen screen;
    private Thread thread;
    private boolean running = false;
    private JFrame frame;
    private BufferedImage image = new BufferedImage(width, height,               
    BufferedImage.TYPE_INT_RGB);  
    private int[] pixels =    
    ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
    public Game() {
        screen = new Screen(width, height);
        frame = new JFrame();

        Dimension size = new Dimension(width * scale, height * scale);
        setPreferredSize(size);
        key = new Keyboard();
        frame.addKeyListener(key);
    }
    public synchronized void start() {

        running = true;
        thread = new Thread(this, "Display");
        thread.start();










    }
    public synchronized void stop(){
        running = false;

        try{

            thread.join();

        }catch(InterruptedException e){

            e.printStackTrace();


        }




    }
    public void run(){
        long lastTime = System.nanoTime();
            long sec = System.currentTimeMillis();
            final double ns = 1000000000.0 / 60.0;
            double delta = 0;
            int frames = 0;
            int ticks = 0;
            while (running) {
                long now = System.nanoTime();
                delta = delta + (now - lastTime) / ns;
                lastTime = now;
                while(delta >= 1) {

                    tick();
                    ticks++;
                    delta--;



                }

                render();
                frames++;
                if(System.currentTimeMillis() - sec > 1000) {
                    sec += 1000;
                    frame.setTitle(title + "        |       " + "Tps: " + ticks + " Fps: " + frames);
                    ticks = 0;
                    frames = 0;

                }
            }
            stop();
        }
    public void tick() {
        key.update();






    }
    public void render() {
        BufferStrategy buffer = getBufferStrategy();
        if(buffer == null){

            createBufferStrategy(3);
            return;

        }
        for(int i = 0; i < pixels.length; i++){


            pixels[i] = screen.pixels[i];


        }
        Graphics g = buffer.getDrawGraphics();
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

        screen.clear();
        screen.render();






        g.dispose();
        buffer.show();
    }
    public static void main(String[] args) {
        Game game = new Game();
        game.frame.setResizable(false);
        game.frame.setTitle(Game.title);
        game.frame.add(game);
        game.frame.pack();
        game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        game.frame.setLocationRelativeTo(null);
        game.frame.setVisible(true);
        game.start();

    }
}

屏幕等级:

package com.pipelinegaming.pixelproblem.graphics;

import java.util.Random;

public class Screen {
private int height, width;
public int [] pixels;
public final int MAP_SIZE = 8;
public final int MAP_SIZE_MASK = MAP_SIZE - 1;
int [] tiles = new int[MAP_SIZE * MAP_SIZE];
public Screen(int width, int height) {
    this.width = width;
    this.height = height;
    pixels = new int [width * height];

    Random random = new Random();
    for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++){
        tiles [i] = random.nextInt(0xffffff);


    }

}
public void clear() {

    for(int i = 1; i < 64 * 64; i++){
        pixels[i] = 0;



    }



}
public void render() {

    for (int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
            pixels[x+y*width] = Sprite.main_page_background.pixels[(x & 15) +      
 (y & 15) * Sprite.main_page_background.SIZE];




        }




    }


   }
   }

键盘类(未使用):

package com.pipelinegaming.pixelproblem.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keyboard implements KeyListener{
private boolean [] keys = new boolean[120];
public boolean up, down, left, right;

public void update() {
    left = keys[KeyEvent.VK_LEFT] || keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_RIGHT] || keys[KeyEvent.VK_D];
    up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    down = keys[KeyEvent.VK_DOWN] || keys[KeyEvent.VK_S];
    for (int i = 0; i < keys.length; i++){
        if(keys[i]) {
        System.out.println("Key: " + i);
        }



    }
}


public void keyTyped(KeyEvent e) {


}


public void keyPressed(KeyEvent e) {

    keys[e.getKeyCode()] = true;

}


public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;

}





























}

SpriteSheet类:

package com.pipelinegaming.pixelproblem.graphics;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
public String path;
public final int SIZE;
public int[] pixels;
public static SpriteSheet all = new     
SpriteSheet("/textures/AllSpriteSheet.png", 256);
public SpriteSheet(String path, int size){
    this.path = path;
    SIZE = size;
    pixels = new int[SIZE * SIZE];
    load();



}
private void load() {
    try {
        BufferedImage image =   
ImageIO.read(SpriteSheet.class.getResource(path));
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0, 0, w, h, pixels, 0, w);
    } catch (IOException e) {

        e.printStackTrace();
    }






}









}

精灵类:

package com.pipelinegaming.pixelproblem.graphics;

public class Sprite {
public final int SIZE;
public int x, y;
public int [] pixels;
public SpriteSheet sheet;

public static Sprite main_page_background = new Sprite(16, 0, 1,      
SpriteSheet.all);


public Sprite (int size, int x, int y, SpriteSheet sheet){

    SIZE = size;
    pixels = new int [SIZE * SIZE];
    this.x = x * size;
    this.y = y * size;
    load();



}
private void load(){
    for (int x = 0; x < SIZE; x++){
        pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) *   
sheet.SIZE];






    }




}


}

我是Java程序员的先驱,但我知道其他一些语言,如果有人可以帮助我,我会真的很赞赏。

2 个答案:

答案 0 :(得分:0)

你在load()方法Sprite class:

中遇到过NullPointerException

看来, sheet.SIZE 会抛出异常;因为你在null对象上调用方法。在执行调用之前,请注意对象创建和初始化。

希望这有帮助。

答案 1 :(得分:0)

我认为NullPointerException是由Sprite类中的行引起的:

sheet.pixels[x + y * SIZE]

因为你试图引用&#34; sheet&#34; (this.y = y * size; load(); )在初始化对象之前。

您可以通过将工作表中的工作表值传递给load()或使用给定值初始化工作表对象来解决此问题,如下所示:

this.y = y * size;
this.sheet = sheet;
load();

变为:

fb_page = Nokogiri::HTML(open("https://www.facebook.com/pages/Singe-Vert/113716970402?fref=ts&rf=131302280307240"))
fb_page.css('span._3tgt._30zy._2l02').text.to_f
相关问题