我有一个程序,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)
我一直在和朋友进行头脑风暴,但仍然没有运气!如果我能得到帮助,将不胜感激。我对编码很陌生,所以我不需要我的代码的所有问题,只需要与问题相关的问题。
编辑:为什么在我使用 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)