java.lang.NullPointerException类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(290)

本文整理了Java中java.lang.NullPointerException类的一些代码示例,展示了NullPointerException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NullPointerException类的具体详情如下:
包路径:java.lang.NullPointerException
类名称:NullPointerException

NullPointerException介绍

[英]Thrown when a program tries to access a field or method of an object or an element of an array when there is no instance or array to use, that is if the object or array points to null. It also occurs in some other, less obvious circumstances, like a throw e statement where the Throwable reference is null.
[中]当程序试图访问对象的字段或方法或数组的元素时,如果没有实例或数组可供使用,即当对象或数组指向null时抛出。它也出现在其他一些不太明显的情况下,比如throw e语句,其中Throwable引用为null。

代码示例

代码示例来源:origin: square/okhttp

public HeldCertificate(KeyPair keyPair, X509Certificate certificate) {
 if (keyPair == null) throw new NullPointerException("keyPair == null");
 if (certificate == null) throw new NullPointerException("certificate == null");
 this.certificate = certificate;
 this.keyPair = keyPair;
}

代码示例来源:origin: ReactiveX/RxJava

/**
 * Creates a NullPointerException instance and sets the given Throwable as its initial cause.
 * @param ex the Throwable instance to use as cause, not null (not verified)
 * @return the created NullPointerException
 */
private static NullPointerException toNpe(Throwable ex) {
  NullPointerException npe = new NullPointerException("Actually not, but can't pass out an exception otherwise...");
  npe.initCause(ex);
  return npe;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void schedulePeriodicallyDirectNullRunnable() {
    try {
      getScheduler().schedulePeriodicallyDirect(null, 5, 10, TimeUnit.MILLISECONDS);
      fail();
    } catch (NullPointerException npe) {
      assertEquals("run is null", npe.getMessage());
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scheduleDirectNullRunnable() {
  try {
    getScheduler().scheduleDirect(null);
    fail();
  } catch (NullPointerException npe) {
    assertEquals("run is null", npe.getMessage());
  }
}

代码示例来源:origin: square/okhttp

/**
 * Sets the handler that can accept cookies from incoming HTTP responses and provides cookies to
 * outgoing HTTP requests.
 *
 * <p>If unset, {@linkplain CookieJar#NO_COOKIES no cookies} will be accepted nor provided.
 */
public Builder cookieJar(CookieJar cookieJar) {
 if (cookieJar == null) throw new NullPointerException("cookieJar == null");
 this.cookieJar = cookieJar;
 return this;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void scheduleDirectWithDelayNullRunnable() {
  try {
    getScheduler().scheduleDirect(null, 10, TimeUnit.MILLISECONDS);
    fail();
  } catch (NullPointerException npe) {
    assertEquals("run is null", npe.getMessage());
  }
}

代码示例来源:origin: square/okhttp

/**
 * Sets the authenticator used to respond to challenges from proxy servers. Use {@link
 * #authenticator} to set the authenticator for origin servers.
 *
 * <p>If unset, the {@linkplain Authenticator#NONE no authentication will be attempted}.
 */
public Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
 if (proxyAuthenticator == null) throw new NullPointerException("proxyAuthenticator == null");
 this.proxyAuthenticator = proxyAuthenticator;
 return this;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nullActionShouldBeCheckedInConstructor() {
  try {
    Flowable.empty().doAfterTerminate(null);
    fail("Should have thrown NullPointerException");
  } catch (NullPointerException expected) {
    assertEquals("onAfterTerminate is null", expected.getMessage());
  }
}

代码示例来源:origin: square/okhttp

private CipherSuite(String javaName) {
 if (javaName == null) {
  throw new NullPointerException();
 }
 this.javaName = javaName;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nullThrowable() {
  SingleSubject<Integer> ss = SingleSubject.create();
  try {
    ss.onError(null);
    fail("No NullPointerException thrown");
  } catch (NullPointerException ex) {
    assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
  }
  ss.test().assertEmpty().cancel();
}

代码示例来源:origin: square/okhttp

/** Change the level at which this interceptor logs. */
public HttpLoggingInterceptor setLevel(Level level) {
 if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
 this.level = level;
 return this;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void onErrorNull() {
    FlowableProcessor<T> p = create();

    try {
      p.onError(null);
      fail("No NullPointerException thrown");
    } catch (NullPointerException ex) {
      assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
    }

    p.test().assertEmpty().cancel();
  }
}

代码示例来源:origin: square/retrofit

/**
 * Set the error response factory to be used when an error is triggered. This factory may only
 * return responses for which {@link Response#isSuccessful()} returns false.
 */
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public void setErrorFactory(Callable<Response<?>> errorFactory) {
 if (errorFactory == null) {
  throw new NullPointerException("errorFactory == null");
 }
 this.errorFactory = errorFactory;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void onNextNull() {
  FlowableProcessor<T> p = create();
  try {
    p.onNext(null);
    fail("No NullPointerException thrown");
  } catch (NullPointerException ex) {
    assertEquals("onNext called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
  }
  p.test().assertEmpty().cancel();
}

代码示例来源:origin: square/retrofit

@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public Builder backgroundExecutor(ExecutorService executor) {
 if (executor == null) throw new NullPointerException("executor == null");
 this.executor = executor;
 return this;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  public void onErrorNull() {
    Subject<T> p = create();

    try {
      p.onError(null);
      fail("No NullPointerException thrown");
    } catch (NullPointerException ex) {
      assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
    }

    p.test().assertEmpty().cancel();
  }
}

代码示例来源:origin: square/okhttp

/**
 * Sets the dispatcher used to match incoming requests to mock responses. The default dispatcher
 * simply serves a fixed sequence of responses from a {@link #enqueue(MockResponse) queue}; custom
 * dispatchers can vary the response based on timing or the content of the request.
 */
public void setDispatcher(Dispatcher dispatcher) {
 if (dispatcher == null) throw new NullPointerException();
 this.dispatcher = dispatcher;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nullValue() {
  SingleSubject<Integer> ss = SingleSubject.create();
  try {
    ss.onSuccess(null);
    fail("No NullPointerException thrown");
  } catch (NullPointerException ex) {
    assertEquals("onSuccess called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
  }
  ss.test().assertEmpty().cancel();
}

代码示例来源:origin: square/okhttp

public Builder url(HttpUrl url) {
 if (url == null) throw new NullPointerException("url == null");
 this.url = url;
 return this;
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nullThrowable() {
  CompletableSubject cs = CompletableSubject.create();
  try {
    cs.onError(null);
    fail("No NullPointerException thrown");
  } catch (NullPointerException ex) {
    assertEquals("onError called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage());
  }
  cs.test().assertEmpty().cancel();;
}

相关文章