gpt4 book ai didi

java.lang.NullPointerException,看不出有什么问题

转载 作者:行者123 更新时间:2023-11-30 07:11:26 24 4
gpt4 key购买 nike

我已经尝试修复这段代码很长时间了,并且由于另一个问题已经在这里发布了!这是我现在遇到的错误...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Maze.Map.getMap(Map.java:34)
at Maze.Board.paint(Board.java:32)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是它来自的代码...抱歉编码不好,但我是一个初学者!

package Maze;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;

public class Map {
private Scanner m;
private String map[] = new String[14];
private Image floor,
wall;

public Map(){

ImageIcon img = new ImageIcon("C://Test//MazeGame//floor.jpg");
floor = img.getImage();
img = new ImageIcon("C://Test//MazeGame//wall.jpg");
wall = img.getImage();

openFile();
readFile();
closeFile();
}

public Image getFloor(){
return floor;
}
public Image getWall(){
return wall;
}

public String getMap(int x, int y){
String index = map[y].substring(x, x + 1);
return index;
}

public void openFile(){

try{
m = new Scanner(new File("C://Test//MazeGame//Map.txt"));
}catch(Exception e){
System.out.print("Error Loading Map!");
}
}

public void readFile(){
for(int i = 0; i < 14; i++){
while(m.hasNext()){
map[i] = m.next();
}
}
}


public void closeFile(){
m.close();
}

}

最佳答案

map[y]null,这就是您收到此异常的原因。当您初始化一个数组时,它会分配大小并且所有位置(索引)都是 null。您需要使所有索引都引用声明的数组类型的对象。即这里是 String

关于java.lang.NullPointerException,看不出有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21219131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com