奇怪的空指针

时间:2011-04-06 00:08:05

标签: java pointers null nullpointerexception

我得到了:

Exception in thread "main" java.lang.NullPointerException
    at Display.readFile(Display.java:55)
    at Display.<init>(Display.java:36)
    at Expordium.play(Expordium.java:15)
    at Expordium.main(Expordium.java:8)

这是我的一类代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.HTMLDocument.Iterator;

import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

public class Display extends JPanel implements Runnable{

private JFrame f = new JFrame("Expordium");
private Collection<Block> elements = new ArrayList<Block>();

private Wall wall;

private String file;
private Expordium game;
private String[][] strLevel;



public void start(Expordium g){
    strLevel = new String[30][10];
    game=g;
    f.getContentPane().add(new Display());
    f.setSize(960 , 320);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}
Display(){
    readFile();
    for(int i=0; i<30; i++){
        for(int j=0; j<30; j++){
            if(strLevel[i][j].equals("*")){
                elements.add(wall=new Wall(i*16,j*16,true,"C:/Program Files (x86)/Software/eclipse/Programs/Expordium/Picture/Block/Grass-T.PNG"));
            }
        }
    }
}

public void run() {
    repaint();
}

public void paint(Graphics g){
    g.setColor(Color.WHITE);
    java.util.Iterator<Block> iterator = elements.iterator();
    while(iterator.hasNext())
        ((Block) iterator.next()).draw(g);

}

private void readFile() {
    int num=0;
    String loc ="C:/Program Files (x86)/Software/eclipse/Programs/Expordium/Level/Level ";
    loc.concat(game.getLevel());
    loc.concat(".txt");
    File Level = new File(loc);
    file=TextFile.getContents(Level);
    for(int i=0; i<30; i++){
        for(int j=0; j<30; j++){
            strLevel[i][j]=file.substring(num, num+1);
            num++;
        }
    }
}

}

我也在线上

loc.concat(名为 “txt”);

strLevel [i] [j] = file.substring(num,num + 1);

EDIT !!!!!!! 我将代码更改为

    private void readFile() {
    int num=0;
    String loc ="C:/Program Files (x86)/Software/eclipse/Programs/Expordium/Level/Level 1.txt";
    File Level = new File(loc);
    file=TextFile.getContents(Level);
    for(int i=0; i<30; i++){
        for(int j=0; j<30; j++){
            System.out.println(file);
            strLevel[i][j]=file.substring(num, num+1);
            num++;
        }
    }
}

现在我


  • *
  • *
  • *
  • *
  • *
  • *
  • * s e

线程“main”java.lang.NullPointerException中的异常     在Display.readFile(Display.java:66)     在Display。(Display.java:36)     在Expordium.play(Expordium.java:15)     在Expordium.main(Expordium.java:8)

1 个答案:

答案 0 :(得分:1)

简短回答: 当你试图调用game.getLevel()时,游戏为空。

更长的回答: 在Display的构造函数中,你做的第一件事(在做任何游戏之前将为null)是调用readFile()。在readFile()中,在设置等于任何东西的游戏之前调用game.getLevel()。您似乎想要开始设置游戏实例,但在尝试使用它之前尚未调用它,因此这是您的NPE的来源。