java.lang.NullPointerException,看不出什么错

时间:2014-01-19 16:04:56

标签: java swing

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

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

}

5 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

您不能初始化地图数组。这一行为14个字符串分配空间,它不会将它们初始化为一个值(即它们默认为null)。

String map[] = new String[14]; // <-- 14 Null strings.

readFile方法看起来应该是这样的

public void readFile(){        
  for(int i = 0; i < 14; i++){
    if (! m.hasNext()) {
      // Alert the user!
      System.err.println("The map file stopped at entry: " + i);
      break;
    }
    map[i] = m.next();
  }
  // to help in debugging.
  System.out.println("The map is: " + java.util.Arrays.toString(map));
}

如果您仍然遇到问题,我会向getMap添加一些消息,其中包含类似的内容

public String getMap(int x, int y) {
  System.out.printf("getMap('%d', '%d')\n", x, y);
  if (map != null && map[y] != null) {
    String index = map[y].substring(x, x + 1);
    return index;
  }
  System.out.println("Returning null");
  return null;
}

答案 2 :(得分:0)

您尚未使用map值填充String数组。

for(int i = 0;i<map.length;i++){
map[i] = "somevalue";
}

例外原因

map[y]在您的情况下给出了null,依此类推null.substring()您获得了NPE。

尝试调试以找出获得NPE时y的实际值。或者使用System.out.println("Value of y is" + y);找出您获得null的索引。

答案 3 :(得分:0)

可能的原因是,您使用索引(getMap())调用y方法,以使map[y]变为null。这可能是因为绑定map数组只有14值!重新检查readFile()方法。

答案 4 :(得分:0)

您只需定义String数组: private String map [] = new String [14];

但你没有初始化这个字符串数组中的元素,map [0],map [1],...