Foros del Web » Programación para mayores de 30 ;) » Java »

Error Exception in thread "main" java.lang.NullPointerException

Estas en el tema de Error Exception in thread "main" java.lang.NullPointerException en el foro de Java en Foros del Web. Hola que tal estoy haciendo un programa para agregar libros y buscarlos, el siguiente error me salta cuando busco un libro: Código: Exception in thread ...
  #1 (permalink)  
Antiguo 29/03/2016, 03:18
 
Fecha de Ingreso: agosto-2009
Mensajes: 292
Antigüedad: 14 años, 8 meses
Puntos: 5
Error Exception in thread "main" java.lang.NullPointerException

Hola que tal estoy haciendo un programa para agregar libros y buscarlos, el siguiente error me salta cuando busco un libro:

Código:
Exception in thread "main" java.lang.NullPointerException
	at actividad12.Actividad12.buscar(Actividad12.java:62)
	at actividad12.Actividad12.main(Actividad12.java:137)
C:\Users\User\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
¿Alguien sabe porque se produce este error y sabe como solucionarlo?

Código Java:
Ver original
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package actividad12;
  7.  
  8. import java.io.*;
  9.  
  10. /**
  11.  *
  12.  * @author Me
  13.  */
  14. public class Actividad12 {
  15.  
  16.     public static BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
  17.     public static String libro[][] = new String[5][5]; //[registro][titulo][autor][editorial][año]
  18.     public static int numLibro = 0;
  19.    
  20.     public static void agregar() {
  21.         if(numLibro < 5) {
  22.             for(int j = 0; j < 5; j++) {
  23.                 switch(j) {
  24.                     case 0:
  25.                         System.out.print("Registro: ");
  26.                         break;
  27.                     case 1:
  28.                         System.out.print("Titulo: ");
  29.                         break;
  30.                     case 2:
  31.                         System.out.print("Autor: ");
  32.                         break;
  33.                     case 3:
  34.                         System.out.print("Editorial: ");
  35.                         break;
  36.                     case 4:
  37.                         System.out.print("Año: ");
  38.                         break;
  39.                 }
  40.                
  41.                 try {
  42.                    libro[numLibro][j] = entrada.readLine();
  43.                 }
  44.                 catch(IOException e) {  }          
  45.             }
  46.            
  47.             numLibro++;
  48.         } else {
  49.             System.out.println();
  50.             System.out.println("Ya se agregaron los 5 libros");
  51.             System.out.println();
  52.         }
  53.        
  54.     }
  55.    
  56.     public static void buscar(int opcion, String busqueda) {
  57.        
  58.         switch(opcion) {
  59.             case 1: //titulo
  60.                 for(int i = 0; i < libro.length; i++) {
  61.                     for(int j = 0; j < 5; j++) {
  62.                         if(libro[i][1].equals(busqueda)) {
  63.                             switch(j) {
  64.                                 case 0:
  65.                                     System.out.print("Registro: ");
  66.                                     System.out.println(libro[i][j]);
  67.                                     break;
  68.                                 case 1:
  69.                                     System.out.print("Titulo: ");
  70.                                     System.out.println(libro[i][j]);
  71.                                     break;
  72.                                 case 2:
  73.                                     System.out.print("Autor: ");
  74.                                     System.out.println(libro[i][j]);
  75.                                     break;
  76.                                 case 3:
  77.                                     System.out.print("Editorial: ");
  78.                                     System.out.println(libro[i][j]);
  79.                                     break;
  80.                                 case 4:
  81.                                     System.out.print("Año: ");
  82.                                     System.out.println(libro[i][j]);
  83.                                     break;
  84.                             }
  85.                         }
  86.                     }
  87.                    
  88.                     System.out.println();
  89.                 }
  90.                 break;
  91.                
  92.             case 2: //autor
  93.                 break;
  94.                
  95.             case 3: //año
  96.                 break;
  97.         }    
  98.     }
  99.    
  100. /**
  101.  * @param args the command line arguments
  102. */
  103.    
  104.     public static void main(String[] args) {
  105.         try {      
  106.            
  107.             String query;
  108.             int opcion, opcionBuscar;
  109.        
  110.             do {
  111.                 System.out.println("1) Agregar libro");
  112.                 System.out.println("2) Buscar");
  113.                 System.out.println("3) Salir");
  114.                 System.out.print("Opcion: ");
  115.                 opcion = Integer.parseInt(entrada.readLine());
  116.                
  117.                 switch(opcion) {
  118.                     case 1:                      
  119.                         agregar();
  120.                         break;
  121.                     case 2:
  122.                         do {
  123.                             System.out.println("1) Autor");
  124.                             System.out.println("2) Titulo");
  125.                             System.out.println("3) Año");
  126.                             System.out.println("4) Regregar");
  127.                             System.out.print("Opcion: ");
  128.                             opcionBuscar = Integer.parseInt(entrada.readLine());
  129.                            
  130.                             switch(opcionBuscar) {
  131.                                 case 1:
  132.                                     break;
  133.                                    
  134.                                 case 2:
  135.                                     System.out.print("Titulo del libro: ");
  136.                                     query = entrada.readLine();
  137.                                     buscar(1, query);  
  138.                                     break;
  139.                                    
  140.                                 case 3:
  141.  
  142.                                     break;
  143.                             }
  144.  
  145.                         } while(opcionBuscar != 4);
  146.                 }      
  147.             } while(opcion != 3);
  148.            
  149.  
  150.                              
  151.         }
  152.         catch(IOException e) { }
  153.        
  154.     }
  155.    
  156. }

Saludos y gracias de antemano
  #2 (permalink)  
Antiguo 29/03/2016, 11:41
 
Fecha de Ingreso: enero-2009
Ubicación: Kandor
Mensajes: 209
Antigüedad: 15 años, 3 meses
Puntos: 11
Respuesta: Error Exception in thread "main" java.lang.NullPointerException

A mi me parece que esta casilla no existe libro[i][1] porque estas instrucciones
if(numLibro < 5) { y numLibro++; estan fuera del for y nunca llena el arreglo libro, entonces tratas de acceder a null aqui libro[i][1], linea 62

Etiquetas: exception, main, netbeans, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:24.