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

public static File getFile(File directory, String[] names) {
    if (directory == null) {
        throw new NullPointerException("directorydirectory must not be null");
    }//www.j  a  va 2s . c  o m
    if (names == null) {
        throw new NullPointerException("names must not be null");
    }
    File file = directory;
    for (String name : names) {
        file = new File(file, name);
    }
    return file;
}

From source file:Main.java

public static void checkNoNullElements(Collection<?> collection, String name) {
    if (collection == null)
        throw new NullPointerException(name);

    int index = 0;
    for (Object element : collection) {
        if (element == null)
            throw new NullPointerException(name + "[" + index + "]");
        index++;/*w ww. j ava  2  s.  com*/
    }
}

From source file:Main.java

public static int indexOf(byte[] datas, int start, byte[] t) {

    if (datas == null || t == null) {
        throw new NullPointerException("source or target array is null!");
    }//w  w  w . j  ava2 s  . co m

    int index = -1;
    int len = datas.length;
    int tlen = t.length;

    if (start >= len || len - start < tlen) {
        return -1;
    }

    while (start <= len - tlen) {
        int i = 0;
        for (; i < tlen; i++) {
            if (datas[start + i] != t[i]) {
                break;
            }
        }

        if (i == tlen) {
            index = start;
            break;
        }

        start++;
    }

    return index;
}

From source file:Main.java

public static Context getContext() {
    if (context != null)
        return context;
    throw new NullPointerException("u should init first");
}

From source file:Main.java

public static String[] splitTitleAndContent(String src) {
    if (src == null) {
        throw new NullPointerException("Helper: splitTitleAndContent(src) - src cannot be null");
    }/*w  ww. j a v  a  2  s.c o m*/

    final int srcLength = src.length();
    String[] out = new String[2];

    int firstNewLinePosition = src.indexOf('\n');

    //the given String contains only one line
    if (firstNewLinePosition == -1) {
        out[0] = src;
        out[1] = "";
    } else {
        //extract the title
        out[0] = src.substring(0, firstNewLinePosition);
        //extract the content
        out[1] = src.substring(firstNewLinePosition + 1, srcLength);
    }

    return out;
}

From source file:Main.java

public static int bytesToInt(boolean asc, byte... bytes) {
    if (null == bytes) {
        throw new NullPointerException("bytes is null!");
    }/*from  ww  w .j  a  va 2  s.  c  o  m*/
    final int length = bytes.length;
    if (length > 4) {
        throw new IllegalArgumentException("Illegal length!");
    }
    int result = 0;
    if (asc)
        for (int i = length - 1; i >= 0; i--) {
            result <<= 8;
            result |= (bytes[i] & 0x000000ff);
        }
    else
        for (int i = 0; i < length; i++) {
            result <<= 8;
            result |= (bytes[i] & 0x000000ff);
        }
    return result;
}

From source file:Main.java

public static boolean validateNodeName(Node node, String nodeName) {
    if (node == null)
        throw new NullPointerException("Node cannot be null!");
    if (nodeName == null)
        throw new NullPointerException("Nodename cannot be null!");
    return nodeName.equals(node.getNodeName());
}

From source file:Main.java

public static boolean isHorizontalBitmap(Bitmap bitmap) {
    if (bitmap == null)
        throw new NullPointerException("Bitmap can not be null!");
    return bitmap.getWidth() < bitmap.getHeight();
}

From source file:Main.java

public static ActivityManager getActivityManager(Context context) {
    if (context == null) {
        throw new NullPointerException("context can not be empty");
    }//from   w ww. j  av a 2 s  .  c  om
    return (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

From source file:Main.java

public static <T> T nonNull(T value, String msg) {
    if (value == null) {
        throw new NullPointerException(msg);
    }/*ww  w  . j  a v  a2s .  c  o m*/
    return value;
}