Error java.lang.NullPointerExcept porque?

Ya casi casi, me queda un poco del codigo, pero me salta un error es este:
Exception in thread "main" java.lang.NullPointerException
no se donde esta el fallo, me ayudas?. Este el codigo: esta al final del codigo cas,i donde empieza el for. Muchas Gracias
import java.io.*;
class NInternal {
    Node[] next;
}
class NList {
    String w;
    NList[] next;
}
class Node {
    char c;
    NInternal[] i = new NInternal[10];
    NList e = new NList();
}
public class Main {
    static public char getChar() throws IOException {
        char c = (char) System.in.read();
        input();
        return c;
    }
    static public void input() throws IOException {
        while ((char) System.in.read() != '\n');
    }
    static public int initNode(Node n, char l) throws IOException {
        // Inicializar el nodo a insertar
        n.e = null;
        n.c = l;
        return 0;
    }
    static public int insertNode(char l, int r, Node root) throws IOException {
        // Inicializar el nodo a insertar
        Node newNode = new Node();
        if (initNode(newNode, l) != 0) {
            System.out.println("Fallo al inicializar el Nodo\n");
        } else {
            // En caso de que sea el primer nodo a insertar.
            if (root == null) {
                System.out.println("Se va a insertar el nodo raiz \n");
                root = newNode;
            } else { // Cualquier otro nodo despues del raiz.
            }
        }
        return r;
    }
    public static void main(String[] args) throws IOException {
        char res;
        Node root = null;
        String s = "";
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);
        do {
            System.out.println("Inserte una palabra y pulse ENTER");
            try {
                s = reader.readLine();
                s = s.toLowerCase();
            } catch (Exception e) {
            }
            System.out.println("Escribiste: " + s + "\n");
            int n[] = null;
           // n = new int[27];
            int tam = s.length();
            int index = 0;
            int max = 0;
            char l = 0;
        char letras[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','ñ','o','p','q','r','s','t','u','v','w','x','y','z'};
        for (int i=0; i<letras.length; i++){
            max= contar(s,letras);
            if (max>0) {
                    System.out.println("El texto contiene " + max + " veces la letra " + letras);
                }
            if (max>index){
                l=letras;
                //index=max;
                max=index; // el mayor es m
            }
        }
                    System.out.println("La letra mas alta del alfabeto  es la "+l+"");
            for (index = 0; index <= max; index++) {
                if (insertNode((char)n[index],index, root) != 0) {
                    System.out.println("Se inserto el caracter " + index + " con: " + n[index] + " repeticiones");
                }
                if (n[index] != 0) {
                    System.out.println("Cantidad de letras " + index + " en la frase es:" + n[index]);
                }
            }
            //exter[mayor] = mayor;
            System.out.println("Quiere insertar otra palabra? S/N");
            res = getChar();
        } while (res !=...

1 respuesta

Respuesta
1
Lo tienes bien
Solo que tienes un vector con comentarios
Quitale los comentarios, aqui te dejo con 'negritas' como me funciono a mi...
Unas simples DIAGONALES. cualquier cosa hay me dices,,,,...
import java.io.*; 
class NInternal { 
    Node[] next; 

class NList { 
    String w; 
    NList[] next; 

class Node { 
    char c; 
    NInternal[] i = new NInternal[10]; 
    NList e = new NList(); 

public class Main { 
    static public char getChar() throws IOException { 
        char c = (char) System.in.read(); 
        input(); 
        return c; 
    } 
    static public void input() throws IOException { 
        while ((char) System.in.read() != '\n'); 
    } 
    static public int initNode(Node n, char l) throws IOException { 
        // Inicializar el nodo a insertar 
        n.e = null; 
        n.c = l; 
        return 0; 
    } 
    static public int insertNode(char l, int r, Node root) throws IOException { 
        // Inicializar el nodo a insertar 
        Node newNode = new Node(); 
        if (initNode(newNode, l) != 0) { 
            System.out.println("Fallo al inicializar el Nodo\n"); 
        } else { 
            // En caso de que sea el primer nodo a insertar. 
            if (root == null) { 
                System.out.println("Se va a insertar el nodo raiz \n"); 
                root = newNode; 
            } else { // Cualquier otro nodo despues del raiz. 
            } 
        } 
        return r; 
    } 
    public static void main(String[] args) throws IOException { 
        char res; 
        Node root = null; 
        String s = ""; 
        InputStreamReader input = new InputStreamReader(System.in); 
        BufferedReader reader = new BufferedReader(input); 
        do { 
            System.out.println("Inserte una palabra y pulse ENTER"); 
            try { 
                s = reader.readLine(); 
                s = s.toLowerCase(); 
            } catch (Exception e) { 
            } 
            System.out.println("Escribiste: " + s + "\n"); 
            int n[] = null; 
            n = new int[27]; <<<<<<<<<<<<<<<<<<ESTE 
            int tam = s.length(); 
            int index = 0; 
            int max = 0; 
            char l = 0; 
        char letras[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','ñ','o','p','q','r','s','t','u','v','w','x','y','z'}; 
        for (int i=0; i<letras.length; i++){ 
            max= contar(s,letras); 
            if (max>0) { 
                    System.out.println("El texto contiene " + max + " veces la letra " + letras); 
                } 
            if (max>index){ 
                l=letras; 
                //index=max; 
                max=index; // el mayor es m 
            } 
        } 
                    System.out.println("La letra mas alta del alfabeto  es la "+l+""); 
            for (index = 0; index <= max; index++) { 
                if (insertNode((char)n[index],index, root) != 0) { //ERROR DE VALOR NULO
                    System.out.println("Se inserto el caracter " + index + " con: " + n[index] + " repeticiones"); 
                } 
                if (n[index] != 0) { 
                    System.out.println("Cantidad de letras " + index + " en la frase es:" + n[index]); 
                } 
            } 
            //exter[mayor] = mayor; 
            System.out.println("Quiere insertar otra palabra? S/N"); 
            res = getChar(); 
        } while (res != ('n')); 

    private static int contar(String s, char c) { 
        return s.replaceAll("[^"+c+"]","").length(); 
    } 
    }

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas