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

/**
 * Creates and returns an unmodifiable List that wraps the given array.
 * Changes to the array will be reflected in the List.
 * @param arr The array to wrap as a List
 * @param fillValue The value to use in place of Float.NaNs
 * @return an unmodifiable List that wraps the array
 * @throws NullPointerException if the array is null
 *///  ww  w  . j  a v  a  2  s  .  com
public static List<Float> listFromFloatArray(final float[] arr, final Float fillValue) {
    if (arr == null)
        throw new NullPointerException("array cannot be null");
    return new AbstractList<Float>() {
        @Override
        public Float get(int index) {
            float val = arr[index];
            if (Float.isNaN(val)) {
                return fillValue;
            } else {
                return val;
            }
            //                return Float.isNaN(val) ? fillValue : val;
        }

        @Override
        public int size() {
            return arr.length;
        }
    };
}

From source file:Main.java

/**
 * Returns a cached thread-local {@link CharsetEncoder} for the specified
 * <tt>charset</tt>./*w ww . j  a  v  a 2s.co  m*/
 */
public static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetEncoder> map = encoders.get();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }

    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}

From source file:Main.java

/**
 * Returns a cached thread-local {@link CharsetDecoder} for the specified
 * <tt>charset</tt>.//w  ww.  java  2s  .  c o m
 */
public static CharsetDecoder getDecoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetDecoder> map = decoders.get();
    CharsetDecoder d = map.get(charset);
    if (d != null) {
        d.reset();
        d.onMalformedInput(CodingErrorAction.REPLACE);
        d.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return d;
    }

    d = charset.newDecoder();
    d.onMalformedInput(CodingErrorAction.REPLACE);
    d.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, d);
    return d;
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null)
        throw new NullPointerException("bitmap ist null!");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, bos);
    return bos.toByteArray();
}

From source file:Main.java

/**
 * Capitalize the first character of the given string.
 *
 * @param string     String to capitalize.
 * @return           Capitalized string.
 *
 * @throws IllegalArgumentException    String is <kk>null</kk> or empty.
 *///ww w.  java 2  s  .  co  m
public static String capitalize(final String string) {
    if (string == null)
        throw new NullPointerException("string");
    if (string.equals(""))
        throw new NullPointerException("string");

    return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}

From source file:com.dattack.dbping.report.MetricName.java

/**
 * Creates a MetricName from its value./*from ww w .ja v a2 s .  com*/
 *
 * @param text
 *            the metric name
 * @return the MetricName
 */
public static MetricName parse(final String text) {

    if (text == null) {
        throw new NullPointerException("Unable to parse a 'null' value as a metric name"); // NOPMD by cvarela on 20/02/16 19:29
    }

    final String[] tokens = text.split(":");
    if (tokens.length > 3) {
        throw new IllegalArgumentException(String.format("Unable to parse the metric name (value: %s)", text));
    }

    return new MetricName(tokens);
}

From source file:Main.java

/**
 * Splits a string around matches of the given delimiter character.
 *
 * Where applicable, this method can be used as a substitute for
 * <code>String.split(String regex)</code>, which is not available
 * on a JSR169/Java ME platform.//  w w  w  .  j  a  v a2s .  co m
 *
 * @param str the string to be split
 * @param delim the delimiter
 * @throws NullPointerException if str is null
 */
static public String[] split(String str, char delim) {
    if (str == null) {
        throw new NullPointerException("str can't be null");
    }

    // Note the javadoc on StringTokenizer:
    //     StringTokenizer is a legacy class that is retained for
    //     compatibility reasons although its use is discouraged in
    //     new code.
    // In other words, if StringTokenizer is ever removed from the JDK,
    // we need to have a look at String.split() (or java.util.regex)
    // if it is supported on a JSR169/Java ME platform by then.
    StringTokenizer st = new StringTokenizer(str, String.valueOf(delim));
    int n = st.countTokens();
    String[] s = new String[n];
    for (int i = 0; i < n; i++) {
        s[i] = st.nextToken();
    }
    return s;
}

From source file:Main.java

/**
 * Adds an element to the collection unless the element is null.
 *
 * @param <T>  the type of object the {@link Collection} contains
 * @param collection  the collection to add to, must not be null
 * @param object  the object to add, if null it will not be added
 * @return true if the collection changed
 * @throws NullPointerException if the collection is null
 * @since 3.2/*from   ww w  .  j a v  a  2  s .c  o m*/
 */
public static <T> boolean addIgnoreNull(final Collection<T> collection, final T object) {
    if (collection == null) {
        throw new NullPointerException("The collection must not be null");
    }
    return object != null && collection.add(object);
}

From source file:fr.univlorraine.mondossierweb.utils.PropertyUtils.java

/** Retourne l'url de l'application */
public static String getAppUrl() {
    String value = System.getProperty("context.app.url");
    if (!StringUtils.hasText(value))
        throw new NullPointerException("app.url cannot be null !");
    return value;
}

From source file:ListUtil.java

/**
 * Splits the list./*from  w ww.  j a  v  a 2  s.  com*/
 * 
 * @param <T>
 *            the type of list element
 * @param list
 *            the list
 * @param size
 *            the piece size
 * @return the split lists.
 * @throws NullPointerException
 *             if the list parameter is null
 * @throws IllegalArgumentException
 *             if the size parameter is less than 1
 */
public static <T> List<List<T>> split(List<T> list, int size)
        throws NullPointerException, IllegalArgumentException {
    if (list == null) {
        throw new NullPointerException("The list parameter is null.");
    }
    if (size <= 0) {
        throw new IllegalArgumentException("The size parameter must be more than 0.");
    }
    int num = list.size() / size;
    int mod = list.size() % size;
    List<List<T>> ret = new ArrayList<List<T>>(mod > 0 ? num + 1 : num);
    for (int i = 0; i < num; i++) {
        ret.add(list.subList(i * size, (i + 1) * size));
    }
    if (mod > 0) {
        ret.add(list.subList(num * size, list.size()));
    }
    return ret;
}