奇怪的空指针

发布于 2024-10-30 18:48:46 字数 3001 浏览 1 评论 0原文

我得到:

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);

编辑!!!!!!! 我将代码更改为

    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++;
        }
    }
}

现在我得到


  • *
  • *
  • *
  • *
  • *
  • *
  • *

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

I'm getting:

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)

This is one class of my code:

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++;
        }
    }
}

}

Also I get it on lines

loc.concat(".txt");

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

EDIT!!!!!!!
I changed the code to

    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++;
        }
    }
}

Now i get


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


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-11-06 18:48:46

简短回答:
当您尝试调用 game.getLevel() 时,game 为 null。

更长的答案:
在 Display 的构造函数中,您要做的第一件事(在对将为 null 的游戏执行任何操作之前)是调用 readFile()。在 readFile() 中,您可以在将 game 设置为任何值之前调用 game.getLevel()。看来您想要开始设置游戏实例,但在尝试使用它之前尚未调用它,因此这就是您的 NPE 的来源。

Short answer:
game is null when you try to call game.getLevel().

Longer answer:
In Display's constructor, the first thing you do (before doing anything with game which will be null) is call readFile(). In readFile(), you invoke game.getLevel() prior to setting game equal to anything. It appears that you want start to set the game instance, but it hasn't been called prior to trying to use it, so that's the source of your NPE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文