throw nullpointerexception java code example

Example 1: how to use a try catch for a null pointer exception in java

int n;//decalred as null
n = n + 1;//null pointer exception
/////////////////////////////////////
int n = 5;
n = n + 1;//no null pointer exception

Example 2: exception is null

//Bug with VS: Exception e == null
//Happens when multiple catch variables are the same
//an example of the solution:
try
{
    // do something
}
catch (WebException webEx) // using a variable named 'webEx' for this catch
{
    Logger.Log("Error while tried to do something. Error: " + webEx.Message); // <-
}
catch (Exception ex) // using a DIFFERENT variable for this one
{
    Logger.Log("Error while tried to do something. Error: " + ex.Message);
}

Example 3: nullpointerexception java

Integer num;
num = new Integer(10);

Example 4: nullpointerexception java

int x;
x = 10;