System.in.read()返回null导致NullPointerException(找不到原因)

时间:2014-11-30 19:01:33

标签: java null system.in

我有一个程序,CMD总是返回:

    :
POKeUTILITY:MAIN_THREAD_STARTED_IN_STATIC_METHOD_MAIN
POKeUTILITY:STARTING_VERSION=1.1
POKeUTILITY:DOWNLOADING_POKeDEX_DATA
POKeUTILITY:READING_POKeDEX_DATA
POKeUTILITY:LOADED_POKeDEX_VERSION:1.3
POKeUTILITY:LOADED_36_STATISTICS
POKeUTILITY:LOADED_7_POKeMON
POKeUTILITY:STARTING_SEARCH_SYSTEM
:To exit type 'exit'
:Enter the name of the pokemon you wish to search.(capitalize the first letter)
:Bulbasaur
Exception in thread "main" java.lang.NullPointerException
    at search.pokemon(main.java:105)
    at main.main(main.java:43)

我和一位朋友一直在集思广益,但仍然没有运气! 如果我能得到帮助,我将非常感激。我对编码很不错,所以我不需要我的代码中的所有问题只是与问题相关的问题。

编辑:为什么它会返回错误 AFTER 我使用System.in.read()输入数据;

Code:

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

class main {
    public static void main(String args[]) {

        String version = new String("1.1");

        int user[] = new int[200];
        String input = "";
        String pokemon[][] = new String[700][10];
        main main = new main();
        dataHandling data = new dataHandling();
        search search = new search();
        int i = 0;
        System.out.println(":");
        System.out.println("POKeUTILITY:MAIN_THREAD_STARTED_IN_STATIC_METHOD_MAIN");
        System.out.println("POKeUTILITY:STARTING_VERSION=" + version);
        System.out.println("POKeUTILITY:DOWNLOADING_POKeDEX_DATA");
        data.download("pokedex");
        System.out.println("POKeUTILITY:READING_POKeDEX_DATA");
        search.load();
        System.out.println("POKeUTILITY:STARTING_SEARCH_SYSTEM");
        pokemon = data.load();
        while (true) {
            input = "";
            int result = 0;
            System.out.println(":To exit type 'exit'");
            System.out.println(":Enter the name of the pokemon you wish to search.(capitalize the first letter)");
            System.out.print(":");
            try {
                while (i < user.length) {
                    user[i] = System.in.read();
                    if (user[i] == 10) break;
                    input += data.convert(user[i]);
                    i++;
                }
            } catch (IOException e) {
                System.out.println("POKeUTILITY:ERROR_READING_CONSOLE_" + e);
            }
            if (search.pokemon(input) == 0) System.out.println("No result"); else {
                result = search.pokemon(input);
                System.out.println("**************");
                System.out.println("#" + pokemon[result][0]);
                System.out.println(pokemon[result][1]);
                System.out.println("**************");
                System.out.println("-Type:" + pokemon[result][2]);
                System.out.println("-Weak to:" + pokemon[result][3]);
                System.out.println("-Resistant to:" + pokemon[result][4]);
                System.out.println("-Abilities:" + pokemon[result][5]);
                System.out.println("-Hidden Abilities:" + pokemon[result][6]);
                System.out.println("**************");
            }
        }
    }
}
class search {
    dataHandling data = new dataHandling();
    boolean doneProcessing = false;
    String dexVersion = new String("");
    String pokemon[][] = new String[700][10];
    int user[] = new int[200];
    char input[] = new char[200];
    int x = 0,y = 0,z = 0,i = 0,pokemonLoaded = 0,dataLoaded = 0,resultDex = 0;
    public void load() {
        while ((data.read(x)) != '%') {
            if ((data.read(x)) == '{') {
                x++;
                while ((data.read(x)) != '}') {
                    dexVersion += data.read(x);
                    x++;
                }
            }
            if ((data.read(x)) == '[') {
                z = 0;
                x++;
                while ((data.read(x)) != ']') {
                    if ((data.read(x)) == '/') {z++;x++;}
                    if ((pokemon[y][z]) == null) pokemon[y][z] = "";
                    pokemon[y][z] += data.read(x);
                    x++;
                }
                y++;
            }
            x++;
        }
        System.out.println("POKeUTILITY:LOADED_POKeDEX_VERSION:" + dexVersion);
        for (int io = 0; io < pokemon.length; io++) {
            for (int o = 0; o < pokemon[0].length; o++) {
                if ((pokemon[io][o]) != null) dataLoaded++;
            }
        }
        System.out.println("POKeUTILITY:LOADED_" + dataLoaded + "_STATISTICS");
        for (int io = 0; io < pokemon.length; io++) {
            if ((pokemon[io][0]) != null) pokemonLoaded++;
        }
        System.out.println("POKeUTILITY:LOADED_" + pokemonLoaded + "_POKeMON");
    }
    public int pokemon(String a) {
        pokemon = data.load();
        if (a.charAt(0) == 'e') {
            if (a.charAt(1) == 'x') {
                if (a.charAt(2) == 'i') {
                    if (a.charAt(3) == 't') {
                        System.exit(0);
                    }
                }
            }
        }
        for (int l = 1; l < pokemon.length; l++) {
            if (pokemon[l][1].contains(a)) resultDex = l;
        }
        return resultDex;
    }
}
class dataHandling {
    String pokemon[][] = new String[700][10];
    int x = 0,y = 0,z = 0;
    public void download(String a) {
        BufferedInputStream dFile;
        FileOutputStream wFile;
        try {
            dFile = new BufferedInputStream(new URL("http://www.harry.technology/" + a).openStream());
            wFile = new FileOutputStream(a);
            int x = 0,y = 0;
            while ((x = dFile.read()) != -1) {
                wFile.write(x);
            }
            wFile.close();
            dFile.close();
        } catch (IOException e) {
            System.out.println("POKeUTILITY:ERROR_" + e);
        }
    }
    public char read(int a) {
        int x = 0,y = 0;
        char input[] = new char[2000];
        String output = new String("");
        try {
            FileReader file = new FileReader("pokedex");
            while ((x = file.read()) != -1) {
                input[y] = convert(x);
                y++;
            }
            file.close();
        } catch (IOException e) {
            System.out.println("POKeUTILITY:ERROR_" + e);
            System.exit(0);
        }
        return input[a];
    }
    public String[][] load() {
        dataHandling data = new dataHandling();
        while ((data.read(x)) != '%') {
            if ((data.read(x)) == '{') {
                x++;
                while ((data.read(x)) != '}') {
                    x++;
                }
            }
            if ((data.read(x)) == '[') {
                z = 0;
                x++;
                while ((data.read(x)) != ']') {
                    if ((data.read(x)) == '/') {z++;x++;}
                    if ((pokemon[y][z]) == null) pokemon[y][z] = "";
                    pokemon[y][z] += data.read(x);
                    x++;
                }
                y++;
            }
            x++;
        }
        return pokemon;
    }
    public char convert(int a) {
        switch (a) {
            case 65: return 'A';
            case 66: return 'B';
            case 67: return 'C';
            case 68: return 'D';
            case 69: return 'E';
            case 70: return 'F';
            case 71: return 'G';
            case 72: return 'H';
            case 73: return 'I';
            case 74: return 'J';
            case 75: return 'K';
            case 76: return 'L';
            case 77: return 'M';
            case 78: return 'N';
            case 79: return 'O';
            case 80: return 'P';
            case 81: return 'Q';
            case 82: return 'R';
            case 83: return 'S';
            case 84: return 'T';
            case 85: return 'U';
            case 86: return 'V';
            case 87: return 'W';
            case 88: return 'X';
            case 89: return 'Y';
            case 90: return 'Z';
            case 97: return 'a';
            case 98: return 'b';
            case 99: return 'c';
            case 100: return 'd';
            case 101: return 'e';
            case 102: return 'f';
            case 103: return 'g';
            case 104: return 'h';
            case 105: return 'i';
            case 106: return 'j';
            case 107: return 'k';
            case 108: return 'l';
            case 109: return 'm';
            case 110: return 'n';
            case 111: return 'o';
            case 112: return 'p';
            case 113: return 'q';
            case 114: return 'r';
            case 115: return 's';
            case 116: return 't';
            case 117: return 'u';
            case 118: return 'v';
            case 119: return 'w';
            case 120: return 'x';
            case 121: return 'y';
            case 122: return 'z';
            case 48: return '0';
            case 49: return '1';
            case 50: return '2';
            case 51: return '3';
            case 52: return '4';
            case 53: return '5';
            case 54: return '6';
            case 55: return '7';
            case 56: return '8';
            case 57: return '9';
            case 58: return ':';
            case 46: return '.';
            case 123: return '{';
            case 125: return '}';
            case 91: return '[';
            case 93: return ']';
            case 47: return '/';
            case 37: return '%';
            case 44: return ',';
            case 45: return '-';
            default: return ' ';
        }
    }
}

它告诉我错误是我打电话给pokemon.search。

如果您有任何更好的方法来改进该方法,请随时了解我仍然是一个初学者。

还试着让词汇保持在低位,我正在努力解决它但不要太担心:)

文件pokedex可在以下位置找到:http://www.harry.technology/pokedex

我还使用另一个小程序来下载主程序的大部分依赖项。我个人排除了我能找到的所有错误,但无论如何这里都是链接:http://www.harry.technology/Update.jar

旁注:跨平台可执行文件的任何想法? (不是.exe,.jar或.app)

1 个答案:

答案 0 :(得分:0)

<强>的NullPointerException
你有一个修复长度为700的数组口袋妖怪。 然后你填写一些元素(1个标题和6个小精灵)。 然后迭代整个数组(第一个除外)来搜索请求的数组:

    for (int l = 1; l < pokemon.length; l++) {
        if (pokemon[l][1].contains(a)) resultDex = l;
    }

首先,如果您找到了请求的小精灵,您可以结束循环 其次,数组pokemon没有完全填充,所以如果你访问一个null的元素并尝试在它上面调用一个方法(包含),它会导致一个NullPointerException (What is a NullPointerException, and how do I fix it?

    for (int l = 1; l < pokemon.length; l++) {
        if (pokemon[l][1] != null && pokemon[l][1].contains(a)) {
            resultDex = l;
            break;
        }
    }

字符串比较
之后,修复了NullPointerException。但是你对Bulbasaur的搜索没有结果,因为你的convert函数会为每个未处理的char返回一个空白。您应该修剪用户输入。如果输入只是 Bulba ,如果你不想获得 Bulbasaur ,也许你应该使用equals而不是contains。 退出检查
使用String.equals()可以使您的退出检查变得更简单 这就是改变的口袋妖怪方法:

public int pokemon(String input) {
    pokemon = data.load();
    String a = input.trim();//trim to remove trailing \r from input
    if("exist".equals(a)) {
        System.exit(0);
    }

    for (int l = 1; l < pokemon.length; l++) {
        if (pokemon[l][1] != null && pokemon[l][1].contains(a)) {
            resultDex = l;
            break;//End the loop
        }
    }
    return resultDex;
}

最后但并非最不重要的是,考虑一下 ChristianHujer 的建议,以摆脱你的转换方法。