length() giving Nullpointerexception

Asked

Viewed 65 times

-2

I am creating a program that will receive multiple inputs from the user and, when receiving a blank string, should stop receiving entries and continue with the program.

The code runs perfectly through my computer but the college’s automated correction system reports an error:

Exception in thread "main" java.lang.NullPointerException
    at SentencaDancante.main(SentencaDancante.java:20)

Line 20 corresponds to while of the code below:

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String entrada = new String(input.readLine());

int contador = 0;

Frases[] objetos = new Frases[100];


while (entrada.length() != 0){

    //the object constructor gets the length and the char[], converted from the "entrada" string.

    objetos[contador] = new Frases(entrada.length(), entrada.toCharArray());

    // defined method in the Frases class
    objetos[contador].converter();

    entrada = input.readLine();

    contador++;

}

I’ve tried to replace the while by a while(entrada != null || entrada.length != 0). But then Vscode starts reporting nullpointeraccess.

2 answers

3


The right thing would be

while(entrada != null && entrada.length() != 0)

because case entrada is null will not attempt to execute the function length and shall not enter into the snare which also makes use of the entrada. It is not possible to perform a function of a null object.

  • I will try to implement, it is the best solution so far. I had not realized that it was necessary the AND and not the OR. Thank you very much

  • @Diogoneiss the AND requires that the two conditions are true to enter within the scope, if the first is not he will not check the second, ie when entrada is null it will not perform entrada.length().

1

An alternative is to change the way you read the data:

String entrada;
int contador = 0;
while ((entrada = input.readLine()) != null) { // enquanto conseguir ler alguma coisa
    if (entrada.length() == 0) // string vazia, sai do loop
        break;

    objetos[contador] = new Frases(entrada.length(), entrada.toCharArray());
    ... faz o que precisar com objetos[contador]

    contador++;
    if (contador == objetos.length) // já encheu o array, sai do loop
        break;
}

Inside the while we have (entrada = input.readLine()) != null - note the parentheses: first I assign the return of readLine in the variable entrada, and then compare if he is null. If so, it’s out of the loop.

Inside the loop I check if the string size is zero (and if so, exits the loop).

I also included a check at the end to see if the counter has already exceeded the size of the array (otherwise it would give a ArrayIndexOutOfBoundsException when the meter reached 100).


On your computer it worked because the System.in is read from the console and you always need to give a ENTER, then the entrada is never null (the readLine removes the line break for ENTER, leaving the empty string).

Already in the automated correction system, probably it takes the entries of some file and passes to your program via pipe or something like that (there is no ENTER typed), then when it comes to the end of stream data, the readLine returns null.

  • 1

    I must admit that I don’t like to change the content of a variable within the loop condition, even more so with a check right after with a break... but this way is better than other alternatives I saw/I can think of

  • @Jeffersonquesado I agree, I also avoid doing this, BufferedReader is one of the few cases where I do...

  • This solution would not work for me, this my teacher is extremely opposed to the use of breaks in the code. I will try to rephrase this output question by placing a && inside the while

  • @Diogoneiss break is a perfectly valid solution, then I would ask the teacher why he is against. But in my opinion, be against without a valid justification is ridiculous, what will say whether to use or not is the context of the code...

  • @hkotsubo he told me once that it had to do with the effect of the break on memory, with something related to batteries or something like that. In addition, a well-designed loop is much more readable for maintenance than a packed breaks and continues. There is also the question of the leap within the programme, similar to that of the goto.

  • @Diogoneiss Tell your teacher some links: that, that and that. In short, break, continue and goto are tools, understand how they work and learn how to use and when not to use. There are legitimate uses of break and if well done, it’s as good as a "well designed loop" (read the links). If your teacher is against "jumps" and worried about memory, then ask him what he thinks about exceptions (if it’s consistent, he should say never use either)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.