java throw nullpointerexception code example
Example 1: how to use a try catch for a null pointer exception in java
int n;
n = n + 1;
int n = 5;
n = n + 1;
Example 2: java throw an exception
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a number");
try {
double nb1 = kb.nextDouble();
if(nb1<0)
throw new ArithmeticException();
else System.out.println( "result : " + Math.sqrt(nb1) );
} catch (ArithmeticException e) {
System.out.println("You tried an impossible sqrt");
}
}
Example 3: exception is null
try
{
}
catch (WebException webEx)
{
Logger.Log("Error while tried to do something. Error: " + webEx.Message);
}
catch (Exception ex)
{
Logger.Log("Error while tried to do something. Error: " + ex.Message);
}
Example 4: nullpointerexception java
Integer num;
num = new Integer(10);
Example 5: NullPointerException
TestObject a = new TestObject();String b = a.getTestResult();b.toString();
Example 6: nullpointerexception java
int x;
x = 10;