2

I am using JDK 1.7, I am checking the code for all conditions of input. if user does not enter any value in the String then it throws a NullPointerException. Is there a way to prevent causing NullPointerException even if the user does not enter any value?

i tried try catch block to catch exception

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
   public static void main(String args[]) throws Exception {

    Scanner s = new Scanner(System.in);    
    int i=s.nextInt();
    System.out.println(i);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    try{
        int length=str.length();  //NullPointerException here

        if(length>=1 && length<=15)
        {
            System.out.println(str);
        }
    }
    catch(NullPointerException e)
    {
        System.out.println("Must enter a string");
    }
   }
}

sample input- 5 null

Expected Output- 5 (Empty string value-> "" but no exception thrown message)

  • NPE is an unchecked exception, so it is bad practice to catch it. Instead just do a simple `null` check – GBlodgett Jan 19 '19 at 19:47
  • 2
    Also why have both a `Scanner` and a `BufferedReader` to read from `System.in`? Why not remove the `BufferedReader` and just have `String str = s.nextLine()`? (And in reference to your likely future question read [this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo)) – GBlodgett Jan 19 '19 at 19:48
  • 1
    I think this is an environment issue. I do not get a `NullPointerException` when I compile and run it in JDK 1.7. Please ensure you are running the right class. – Kedar Mhaswade Jan 19 '19 at 20:00
  • if str is null, calling any method such as .length() will cause a NullPointerException – GothamGirl Jan 19 '19 at 20:34

4 Answers4

3

Java 8

int length = Optional.ofNullable(str).orElse("").length();

Java 7

int length = str == null ? 0 : str.length();

Java 7 + Apache Commons

int length = StringUtils.length(str);

Use Scanner

Use Scanner instead of BufferedReader; scane.nextLine() returns not null string.


public static void main(String... args) {
    try (Scanner s = new Scanner(System.in)) {
        System.out.println(s.nextInt());

        s.nextLine();

        String str = s.nextLine();

        if (str.length() >= 1 && str.length() <= 15)
            System.out.println(str);
    }
}
oleg.cherednik
  • 12,764
  • 2
  • 17
  • 25
0

Instead of try/catch you could check if str is not null before you call str.length()

Scanner s = new Scanner(System.in);
String str = s.nextLine();
if (str != null && str.length() <= 15) {
   System.out.println(str);
} else {
   System.out.println("Must enter a string");
}
Reto
  • 1,220
  • 15
  • 25
0
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
   public static void main(String args[]) throws Exception {

    Scanner s = new Scanner(System.in);    
    int i=s.nextInt();
    System.out.println(i);

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
        if(str!=null && str.length() >=1 && str.length()<=15)
        {
            System.out.println(str);
        }
    }
   }
}
GothamGirl
  • 289
  • 2
  • 12
0

1) Read the documentation - note that BufferedReader.readline can legitimately return null under well-defined circumstances.

2) Write code that can handle the possible null return.