java.lang.NullPointerException.getCause()方法的使用及代码示例

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

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

NullPointerException.getCause介绍

暂无

代码示例

代码示例来源:origin: apache/avro

/** Helper method for adding a message to an NPE. */
protected NullPointerException npe(NullPointerException e, String s) {
 NullPointerException result = new NullPointerException(e.getMessage()+s);
 result.initCause(e.getCause() == null ? e : e.getCause());
 return result;
}

代码示例来源:origin: org.apache.avro/avro

/** Helper method for adding a message to an NPE. */
protected NullPointerException npe(NullPointerException e, String s) {
 NullPointerException result = new NullPointerException(e.getMessage()+s);
 result.initCause(e.getCause() == null ? e : e.getCause());
 return result;
}

代码示例来源:origin: apache/avro

@Override
protected void write(Schema schema, Object datum, Encoder out)
 throws IOException {
 if (datum instanceof Byte)
  datum = ((Byte)datum).intValue();
 else if (datum instanceof Short)
  datum = ((Short)datum).intValue();
 else if (datum instanceof Character)
  datum = (int)(char)(Character)datum;
 else if (datum instanceof Map && ReflectData.isNonStringMapSchema(schema)) {
  // Maps with non-string keys are written as arrays.
  // Schema for such maps is already changed. Here we
  // just switch the map to a similar form too.
  Set entries = ((Map)datum).entrySet();
  List<Map.Entry> entryList = new ArrayList<>(entries.size());
  for (Object obj: ((Map)datum).entrySet()) {
    Map.Entry e = (Map.Entry)obj;
    entryList.add(new MapEntry(e.getKey(), e.getValue()));
  }
  datum = entryList;
 }
 try {
  super.write(schema, datum, out);
 } catch (NullPointerException e) {            // improve error message
  NullPointerException result =
   new NullPointerException("in "+schema.getFullName()+" "+e.getMessage());
  result.initCause(e.getCause() == null ? e : e.getCause());
  throw result;
 }
}

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

@Test
public void sourceThrowsIAE() {
  try {
    Maybe.unsafeCreate(new MaybeSource<Object>() {
      @Override
      public void subscribe(MaybeObserver<? super Object> observer) {
        throw new IllegalArgumentException("Forced failure");
      }
    }).test();
    fail("Should have thrown!");
  } catch (NullPointerException ex) {
    assertTrue(ex.toString(), ex.getCause() instanceof IllegalArgumentException);
    assertEquals("Forced failure", ex.getCause().getMessage());
  }
}

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

fail("Should have thrown!");
} catch (NullPointerException ex) {
  if (!(ex.getCause() instanceof IllegalArgumentException)) {
    fail(ex.getCause().toString() + ": Should have thrown NullPointerException(IllegalArgumentException)");
  fail("Should have thrown!");
} catch (NullPointerException ex) {
  if (!(ex.getCause() instanceof RuntimeException)) {
    fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))");
  if (!(ex.getCause().getCause() instanceof IOException)) {
    fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))");

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

@Test
public void subscribeThrows() {
  try {
    new Single<Integer>() {
      @Override
      protected void subscribeActual(SingleObserver<? super Integer> observer) {
        throw new IllegalArgumentException();
      }
    }.test();
  } catch (NullPointerException ex) {
    if (!(ex.getCause() instanceof IllegalArgumentException)) {
      fail(ex.toString() + ": should have thrown NPE(IAE)");
    }
  }
}

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

@Test
  public void callbackCrash() {
    try {
      Observable.just(1)
      .lift(new ObservableOperator<Object, Integer>() {
        @Override
        public Observer<? super Integer> apply(Observer<? super Object> o) throws Exception {
          throw new TestException();
        }
      })
      .test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.toString(), ex.getCause() instanceof TestException);
    }
  }
}

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

@Test
public void badSourceCompleteDisconnect() {
  BadObservableConnect2 bo = new BadObservableConnect2();
  try {
    bo.refCount()
    .test();
    fail("Should have thrown");
  } catch (NullPointerException ex) {
    assertTrue(ex.getCause() instanceof TestException);
  }
}

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

@Test
public void badSourceSubscribe() {
  BadObservableSubscribe bo = new BadObservableSubscribe();
  try {
    bo.refCount()
    .test();
    fail("Should have thrown");
  } catch (NullPointerException ex) {
    assertTrue(ex.getCause() instanceof TestException);
  }
}

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

@Test
public void badSourceConnect() {
  BadObservableConnect bo = new BadObservableConnect();
  try {
    bo.refCount()
    .test();
    fail("Should have thrown");
  } catch (NullPointerException ex) {
    assertTrue(ex.getCause() instanceof TestException);
  }
}

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

@Test
public void badSourceSubscribe2() {
  BadObservableSubscribe2 bo = new BadObservableSubscribe2();
  Observable<Object> o = bo.refCount();
  o.test();
  try {
    o.test();
    fail("Should have thrown");
  } catch (NullPointerException ex) {
    assertTrue(ex.getCause() instanceof TestException);
  }
}

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

@Test(timeout = 5000)
public void createOnSubscribeThrowsRuntimeException() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Completable c = Completable.unsafeCreate(new CompletableSource() {
      @Override
      public void subscribe(CompletableObserver observer) {
        throw new TestException();
      }
    });
    c.blockingAwait();
    Assert.fail("Did not throw exception");
  } catch (NullPointerException ex) {
    if (!(ex.getCause() instanceof TestException)) {
      ex.printStackTrace();
      Assert.fail("Did not wrap the TestException but it returned: " + ex);
    }
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void subscribeActualThrows() {
  List<Throwable> list = TestHelper.trackPluginErrors();
  try {
    try {
      new BadObservable().test();
      fail("Should have thrown!");
    } catch (NullPointerException ex) {
      if (!(ex.getCause() instanceof IllegalArgumentException)) {
        fail(ex.toString() + ": Should be NPE(IAE)");
      }
    }
    TestHelper.assertError(list, 0, IllegalArgumentException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void badSourceSubscribe() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    BadFlowableSubscribe bo = new BadFlowableSubscribe();
    try {
      bo.refCount()
      .test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.getCause() instanceof TestException);
    }
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void badSourceConnect() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    BadFlowableConnect bf = new BadFlowableConnect();
    try {
      bf.refCount()
      .test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.getCause() instanceof TestException);
    }
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
public void badSourceCompleteDisconnect() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    BadFlowableConnect2 bf = new BadFlowableConnect2();
    try {
      bf.refCount()
      .test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.getCause() instanceof TestException);
    }
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
  public void subscribeActualThrows() {
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
      try {
        new BadFlowable().test();
        fail("Should have thrown!");
      } catch (NullPointerException ex) {
        if (!(ex.getCause() instanceof IllegalArgumentException)) {
          fail(ex.toString() + ": Should be NPE(IAE)");
        }
      }

      TestHelper.assertError(list, 0, IllegalArgumentException.class);
    } finally {
      RxJavaPlugins.reset();
    }
  }
}

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

@Test
  public void unsafeCreateThrowsIAE() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
      Completable.unsafeCreate(new CompletableSource() {
        @Override
        public void subscribe(CompletableObserver observer) {
          throw new IllegalArgumentException();
        }
      }).test();
      fail("Should have thrown!");
    } catch (NullPointerException ex) {
      if (!(ex.getCause() instanceof IllegalArgumentException)) {
        fail(ex.toString() + ": should have thrown NPA(IAE)");
      }

      TestHelper.assertError(errors, 0, IllegalArgumentException.class);
    } finally {
      RxJavaPlugins.reset();
    }
  }
}

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

@Test
public void badSourceSubscribe2() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    BadFlowableSubscribe2 bf = new BadFlowableSubscribe2();
    Flowable<Object> f = bf.refCount();
    f.test();
    try {
      f.test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.getCause() instanceof TestException);
    }
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

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

@Test
  public void callbackCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
      Flowable.just(1)
      .lift(new FlowableOperator<Object, Integer>() {
        @Override
        public Subscriber<? super Integer> apply(Subscriber<? super Object> subscriber) throws Exception {
          throw new TestException();
        }
      })
      .test();
      fail("Should have thrown");
    } catch (NullPointerException ex) {
      assertTrue(ex.toString(), ex.getCause() instanceof TestException);
      TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
      RxJavaPlugins.reset();
    }
  }
}

相关文章