简体   繁体   English

无法从Java中的文本文件读取和排序信息

[英]Unable to read and sort information from a text file in Java

I'm attempting to read a text file using BufferedReader and sort the information within the file. 我正在尝试使用BufferedReader读取文本文件并对文件中的信息进行排序。 The purpose is to load and be able to access information on maps for a game I'm making. 目的是加载并能够访问我正在制作的游戏在地图上的信息。 The problem I have is that when my program tried to sort the information I get a NullPointerException . 我的问题是,当我的程序尝试对信息进行排序时,我得到了NullPointerException

LoadWorld.java: LoadWorld.java:

public class LoadWorld {

    private static String CurrentString;
    private static String[] LinePts;
    private static String Path="";
    private static String Name="";
    private static String Auth="";
    private static String Date="";

    private static boolean Reading = true;
    private static boolean OnMap = false;

    private static int Maps = 0;

    private static Map[] Map;

    public static void Load() {

         try {

                BufferedReader reader = 
                    new BufferedReader(new InputStreamReader(LoadWorld.class.getResourceAsStream("/Maps/Maps.txt")));

                while(Reading) {

                    CurrentString = reader.readLine();

                    if (CurrentString.equals("{")) {OnMap=true;Maps+=1;}
                    else {Reading=false;}

                    while(OnMap) {



                        CurrentString = reader.readLine();
                        if (!(CurrentString.equals("}"))) {

                            LinePts = CurrentString.split("-");

                            if (LinePts[0].equals("PATH")) {Path=LinePts[1];}
                            else if (LinePts[0].equals("NAME")) {Name=LinePts[1];}
                            else if (LinePts[0].equals("AUTH")) {Auth=LinePts[1];}
                            else if (LinePts[0].equals("DATE")) {Date=LinePts[1];}

                        }
                        else {

                            Map[Maps].Path = Path;
                            Map[Maps].Name = Name;
                            Map[Maps].Auth = Auth;
                            Map[Maps].Date = Date;
                            OnMap=false;

                        }

                    }

                }

                reader.close();         
            }

            catch(IOException e) {

                e.printStackTrace();

            }

    }

The problem occurs in the "else{" section, at "map[maps].path = path;". 该问题出现在“ else {”部分的“ map [maps] .path = path;”处。

Map.java: Map.java:

public class Map {

    public String Path;
    public String Name;
    public String Auth;
    public String Date;
    public String Map;

}

The text file I'm attempting to read: Maps.txt 我尝试读取的文本文件:Maps.txt

{
PATH-Default/Basic.txt
NAME-Basic Map
AUTH-Aelex Esrom
DATE-11/1/15
}

Any help is appreciated! 任何帮助表示赞赏! Thanks in advance for any answers. 预先感谢您的任何答案。

you need to initialize your variable Map 您需要初始化变量Map

Map = new Map[5]; //array of fixed size 5

perhaps you are trying to dynamically add array elements the PHP way but it does not work the same way in Java. 也许您正在尝试以PHP方式动态添加数组元素,但是在Java中它的工作方式不同。 You will either create an array of a fixed size or use ArrayList 您将创建一个固定大小的数组或使用ArrayList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM