fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. new Ideone().executar();
  13. }
  14.  
  15. public void executar(){
  16. Map<String, ClasseProHashMap> mapa = new HashMap<String, ClasseProHashMap>();
  17.  
  18. ClasseProHashMap cp1 = new ClasseProHashMap("Primeiro", "Joao", 100);
  19. ClasseProHashMap cp2 = new ClasseProHashMap("Segundo", "Jose", 200);
  20.  
  21. mapa.put("Primeiro", cp1);
  22. mapa.put("Segundo", cp2);
  23.  
  24. ClasseProHashMap testandoGet = mapa.get("Segundo");
  25.  
  26. testandoGet.imprimir();
  27.  
  28. ClasseProHashMap testandoGetErrado = mapa.get("ChaveErrada");
  29.  
  30. testandoGetErrado.imprimir(); // nullpointerexception
  31. }
  32.  
  33. class ClasseProHashMap{
  34. String chave;
  35. String nome;
  36. int valor;
  37.  
  38. public ClasseProHashMap(String c, String n, int v){
  39. this.chave = c;
  40. this.nome = n;
  41. this.valor = v;
  42. }
  43.  
  44. public void imprimir(){
  45. System.out.println("Chave: " + chave + " Nome: " + nome + " Valor: " + valor);
  46. }
  47. }
  48. }
Runtime error #stdin #stdout #stderr 0.04s 711168KB
stdin
Standard input is empty
stdout
Chave: Segundo  Nome: Jose Valor: 200
stderr
Exception in thread "main" java.lang.NullPointerException
	at Ideone.executar(Main.java:30)
	at Ideone.main(Main.java:12)