مواجهة صفيف مؤشر فارغ عند الحلقات على الرغم من الكائن. كيف يمكنني القبض عليه؟

StackOverflow https://stackoverflow.com/questions/19842584

  •  29-07-2022
  •  | 
  •  

سؤال

أواجه مؤشرًا فارغًا عندما أحلق من خلال صفيف بلدي. هل هناك اختبار يمكنني تشغيله لمعرفة ما قد يسبب هذا الخطأ ... وربما لا توجد قيمة عندما يجب أن يكون هناك؟ يتوقف النظام في Mappiles [0] [33]. هذا هو الرمز:

import java.lang.*;
import java.util.*;
import java.io.*;

public class Map {

MapTile[][] mapTiles;
String imageMap;
String rawMap;

// constructor 
public Map() {
    imageMap = "Map_DragonShrine.jpg";
    rawMap = "Dragon_Shrine.map";
    mapTiles = new MapTile[22][34];
}


// methods
public void loadMapFile() {

    rawMap = file2String(rawMap);

    // array used to hold columns in a row after spliting by space
    String[] mapCols = null;
    // split map using 'bitmap' as delimiter
    String[] mapLines = rawMap.split("bitmap");  
    // assign whatever is after 'bitmap'
    rawMap = mapLines[1];
    // split string to remove comment on the bottom of the file
    mapLines = rawMap.split("#");
    // assign final map
    rawMap = mapLines[0].trim();
    mapLines = rawMap.split("\\n+");

    for(int x = 0; x < mapLines.length; x++) {
        rawMap = mapLines[x] ;
        mapCols = rawMap.split("\\s+");            
        for(int y = 0; y < mapCols.length; y++) {
            mapTiles[x][y] = new MapTile(mapCols[y]);   
        }            
    }   
}       

public void getTileFeatures() {
}

public void checkFeature() {
    for(int x = 0; x < mapTiles.length; x++) {
        for(int y = 0; y < mapTiles[x].length; y++) {
            makeTile(x, y, mapTiles[x][y].getFeature());
            //  java.lang.NullPointerException at mapTiles[0][33]
        }
    }
}

public String file2String(String filename) 
{          
    try{ 
        FileReader fileReader = new FileReader(filename); 
        String fileContents = ""; 
        int i ; 

        while((i =  fileReader.read())!=-1){ 
            char ch = (char)i; 

            fileContents = fileContents + ch;  
        }//end while 

        // System.out.println(fileContents); 
        return fileContents; 
    }
    catch(Exception e){
        System.out.println("File failed to load:" + e); 
        return "Error " + e; 
    } 

}//end file2String

private void makeTile(int x, int y, String features)
{
    System.out.println("line1 " + y + " test");
    //         mapTiles[x][y] = new MapTile();//Assumes your Map class has an array of MapTile objects called mapTiles
    if(features.contains("w")){   mapTiles[x][y].SOLID = true; }
    System.out.println("line2 " + y + " test");
    if(features.contains("s")){   mapTiles[x][y].STATUE = true; }
    System.out.println("line3 " + y + " test");
    if(features.contains("A")){   mapTiles[x][y].VICTORY_A = true; }
    System.out.println("line4 " + y + " test");
    if(features.contains("B")){   mapTiles[x][y].VICTORY_B = true; }
    System.out.println("line5 " + y + " test");
    if(features.contains("a")){   mapTiles[x][y].START_A = true; }
    System.out.println("line6 " + y + " test");
    if(features.contains("b")){   mapTiles[x][y].START_B = true; }
    System.out.println("line7 " + y + " test");
    if(features.contains("h")){   mapTiles[x][y].HAUNTED = true; }
    System.out.println("line8 " + y + " test");
    if(features.contains("d")){   mapTiles[x][y].DIFFICULT = true; }
    System.out.println("line9 " + y + " test");
    if(features.contains("c")){   mapTiles[x][y].SACRED_CIRCLE = true; }
    System.out.println("line10 " + y + " test");
    if(features.contains("u")){   mapTiles[x][y].SUMMONING_CIRCLE = true; }
    System.out.println("line11 " + y + " test");
    if(features.contains("x")){   mapTiles[x][y].EXIT_A = true; }
    System.out.println("line12 " + y + " test");
    if(features.contains("z")){   mapTiles[x][y].EXIT_B = true; }
    System.out.println("line13 " + y + " test");
}//End makeTile

}

هل كانت مفيدة؟

المحلول

يجب عليك تهيئة صفيف Maptile 2D باستخدام الخاص بك mapLines هدف. أنت الآن تفترض أنه سيكون دائمًا 22 × 34.

يمكن أن يتسبب هذا بسهولة في وجود مؤشر فارغ حيث لا يتم تهيئة بعض بلاطات الخرائط. يمكن أن يسبب أيضا indexoutofbounds.

خطأك سيصل إلى ذلك. في مكان ما لا تقوم بتهيئة هذا الفهرس في صفيف maptile.

نصائح أخرى

استخدم أدوات اختبار الوحدة مثل Junit ، Mockito لاختبار فصلك. بهذه الطريقة يمكنك الإعداد MapTile[][] مع قيم مختلفة واختبار إجراء ضدها. يمكن العثور على معلومات عن Mockito هنا

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top