0

I have a program and the CMD always returns:

    :
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)

I have been brainstorming with a friend but still no luck! If i can have help it would be very appreciated. I am decently new to coding so i don't need all the problems with my code just the ones related to the question.

EDIT: Why does it return an error AFTER I enter data using 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 ' ';
        }
    }
}

It tells me the error is when i call the pokemon.search.

If you have any better ways to improve that method feel free to but understand i am still a begginner.

Also try to keep the vocab to a low, i am struggling with it but dont be too worried:)

the file pokedex can be found at : http://www.harry.technology/pokedex

i am also usung another small program to download most of the dependencies for the main program. I have personally ruled out all the errors i can find but here is the link anyway: http://www.harry.technology/Update.jar

Sidenote: Any ideas for a cross platform executable? (not .exe, .jar, or .app)

Harry Saliba
  • 116
  • 1
  • 11

1 Answers1

0

NullPointerException
You have an array pokemon of the fix length 700. Then you fill in a few elements (1 headline and 6 pokemons). Then you iterate over the whole Array(except the first) to search the requested one:

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

First, if you found the requested pokemon, you could end the loop.
Second, the array pokemon is not completely filled, so if you access an element which is null and try to call a method on it (contains), it results in a 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;
        }
    }

String comparison
After that, the NullPointerException is fixed. But your search for Bulbasaur will get no results, because your convert-function returns a blank for every unhandled char. You should trim the user input. And maybe you should use equals instead of contains if you don't want to get Bulbasaur if the input is only Bulba.
Exit check
Your Exit check could be made simpler by using String.equals().
And that is the changed pokemon method:

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

And last but not least, think about the recommendation from ChristianHujer to get rid of your convert method.

Community
  • 1
  • 1
Gren
  • 1,784
  • 1
  • 8
  • 15