Example usage for java.lang NullPointerException NullPointerException

List of usage examples for java.lang NullPointerException NullPointerException

Introduction

In this page you can find the example usage for java.lang NullPointerException NullPointerException.

Prototype

public NullPointerException(String s) 

Source Link

Document

Constructs a NullPointerException with the specified detail message.

Usage

From source file:Main.java

/**
 * Get all the matches for {@code string} compiled by {@code pattern}. If 
 * {@code isGlobal} is true, the return results will only include the 
 * group 0 matches. It is similar to string.match(regexp) in JavaScript.
 * /*from   ww w  .ja  v a 2  s . c  om*/
 * @param pattern the regexp
 * @param string the string
 * @param isGlobal similar to JavaScript /g flag
 * 
 * @return all matches
 */
public static String[] match(Pattern pattern, String string, boolean isGlobal) {
    if (pattern == null) {
        throw new NullPointerException("argument 'pattern' cannot be null");
    }
    if (string == null) {
        throw new NullPointerException("argument 'string' cannot be null");
    }

    List<String> matchesList = new ArrayList<String>();

    Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        matchesList.add(matcher.group(0));
        if (!isGlobal) {
            for (int i = 1, iEnd = matcher.groupCount(); i <= iEnd; i++) {
                matchesList.add(matcher.group(i));
            }
        }
    }

    return matchesList.toArray(new String[matchesList.size()]);
}

From source file:Main.java

public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException {
    if (keyString == null || keyString.length() == 0) {
        throw new NullPointerException("Please give Password");
    }/*  w  w  w . ja  v a  2  s  .  co  m*/

    if (clearText == null || clearText.length <= 0) {
        throw new NullPointerException("Please give clearText");
    }

    try {
        SecretKeySpec skeySpec = getKey(keyString);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // Cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);

        return cipher.doFinal(clearText);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("deprecation")
static String getQueryString(List<Pair<String, String>> pairs) {
    StringBuilder queryBuilder = new StringBuilder();
    boolean first = true;
    for (Pair<String, String> pair : pairs) {
        // This will throw a NullPointerException if you call URLEncoder.encode(null).
        // Instead caught & thrown with description above.
        String value = pair.second;
        if (value == null) {
            // Can't be more specific without jeopardizing security.
            throw new NullPointerException("Malformed Request. Entry has null value for key: " + pair.first);
        }/*from w  ww  . ja  va2s  .  c o  m*/

        if (!first) {
            queryBuilder.append("&");
        }
        queryBuilder.append(pair.first);
        queryBuilder.append("=");
        try {
            queryBuilder.append(URLEncoder.encode(value, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // Fallback
            queryBuilder.append(URLEncoder.encode(value));
        }

        first = false;

    }

    return queryBuilder.toString();
}

From source file:Main.java

public static <T> void checkNotNull(T t, String message) {
    if (t == null)
        throw new NullPointerException(message);
}

From source file:ArrayUtil.java

/**
 * Determines if the passed object is of type array.
 * @param o The object to determine if it is an array.
 * @return true if the passed object is an array.
 * @throws NullPointerException when the passed object is null.
 *//*  w  w  w .ja va 2s  . c o m*/
public static boolean isArray(Object o) throws NullPointerException {
    if (o == null)
        throw new NullPointerException("Object is null: cannot determine if it is of array type.");
    else {
        return o.getClass().isArray();
    }
}

From source file:Main.java

public static <T> T assertNotNull(T reference, @Nullable Object errorMessage) {
    if (reference == null) {
        throw new NullPointerException(String.valueOf(errorMessage));
    }/*from  w w  w  .j  a  v  a 2  s .c o m*/
    return reference;
}

From source file:Main.java

/**
 * Inserts into an ascending sorted list an element.
 *
 * Preconditions: The element has to implement the {@code Comparable}
 * interface and the list have to be sorted ascending. Both conditions will
 * not be checked: At runtime a class cast exception will be thrown
 * if the element does not implement the comparable interface and and if the
 * list is not sorted, the element can't be insert sorted.
 *
 * @param <T>     element type/*from  w  w w. ja v  a 2s  .  c  o  m*/
 * @param list
 * @param element
 */
@SuppressWarnings("unchecked")
public static <T> void binaryInsert(LinkedList<? super T> list, T element) {
    if (list == null) {
        throw new NullPointerException("list == null");
    }

    if (element == null) {
        throw new NullPointerException("element == null");
    }

    boolean isComparable = element instanceof Comparable<?>;

    if (!isComparable) {
        throw new IllegalArgumentException("Not a comparable: " + element);
    }

    int size = list.size();
    int low = 0;
    int high = size - 1;
    int index = size;
    int cmp = 1;

    while ((low <= high) && (cmp > 0)) {
        int mid = (low + high) >>> 1;
        Comparable<? super T> midVal = (Comparable<? super T>) list.get(mid);

        cmp = midVal.compareTo(element);

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        }
    }

    for (int i = low; (i >= 0) && (i < size) && (index == size); i++) {
        Comparable<? super T> elt = (Comparable<? super T>) list.get(i);

        if (elt.compareTo(element) >= 0) {
            index = i;
        }
    }

    list.add(index, element);
}

From source file:Main.java

/**
 * Creates a confirmation dialog that show a pop-up with button labeled as parameters labels.
 *
 * @param ctx                 {@link Activity} {@link Context}
 * @param message             Message to be shown in the dialog.
 * @param dialogClickListener For e.g./*from  w  w  w .  j  ava 2  s  . co  m*/
 *                            <p/>
 * @param positiveBtnLabel    For e.g. "Yes"
 * @param negativeBtnLabel    For e.g. "No"
 *
 */
public static void showDialog(Context ctx, String message, String positiveBtnLabel, String negativeBtnLabel,
        DialogInterface.OnClickListener dialogClickListener) {

    if (dialogClickListener == null) {
        throw new NullPointerException("Action listener cannot be null");
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);

    builder.setMessage(message).setPositiveButton(positiveBtnLabel, dialogClickListener)
            .setNegativeButton(negativeBtnLabel, dialogClickListener).show();
}

From source file:Main.java

public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException {

    if (password == null || password.length() == 0) {
        throw new NullPointerException("Please give Password");
    }/*from w w  w  .  j  ava2  s  .c  o m*/

    if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) {
        throw new NullPointerException("Please give encrypedPwdBytes");
    }

    try {
        SecretKey key = getKey(password);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes));

        return decryptedValueBytes;

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.redhat.che.multitenant.UserCheTenantDataValidator.java

public static void validate(final UserCheTenantData data) {
    if (data == null) {
        LOG.error("'UserCheTenantData' can not be null");
        throw new NullPointerException("'UserCheTenantData' can not be null");
    } else if (StringUtils.isBlank(data.getClusterUrl())) {
        LOG.error("'ClusterUrl' can not be blank: {}", data);
        throw new IllegalArgumentException("'ClusterUrl' can not be blank");
    } else if (StringUtils.isBlank(data.getRouteBaseSuffix())) {
        LOG.error("'RouteBaseSuffix' can not be blank: {}", data);
        throw new IllegalArgumentException("'RouteBaseSuffix' can not be blank");
    } else if (StringUtils.isBlank(data.getNamespace())) {
        LOG.error("'Namespace' can not be blank: {}", data);
        throw new IllegalArgumentException("'Namespace' can not be blank");
    }// w w w  .j a va2s  .  c  o m
}