0
In the code snippet below, you are supposed to compare the value of the items in the array linhas with the String inserted by the user p. It’s working, but there’s always the error:
Stack trace - Exception in thread "main" java.lang.Nullpointerexception At Mainswitch.main(Mainswitch.java:229)
The code:
String guardar;
String lista[] = new String [tamMax];
System.out.println("Insira a palavra que pretende pesquisar:");
p = reader.next();
for(int i = 0; i < linhas.length; i++)
{
guardar = linhas[i];
lista = guardar.split(" "); //linha do erro
for(int k = 0; k < lista.length; k++)
{
if(lista[k].equals(p))
{
System.out.println("Linha " + i + ": " + linhas[i]);
}
}
}
Place the stacktrace and point with a comment in the code the problem line.
– Jefferson Quesado
Exception in thread "main" java.lang.Nullpointerexception At mainswitch.main(Mainswitch.java:229) The error is in the split line - list = save.(" ");
– João Pereira
Always good to check this: https://answall.com/q/63617/64969; read all the answers. The problem is that its vector
linhaswas initialized but not populated, therefore for some indexiwe have tolista[i] == null– Jefferson Quesado
I get it.. And what’s the best way to solve this?
– João Pereira
By the way, if you want to use comments
//, leave the code on the left, otherwise the code becomes part of the comment– Jefferson Quesado
Done, my rsrs mistake
– João Pereira
After this line
guardar = linhas[i];tries to putif( guardar == null ) continue;– Icaro Martins