Tag: nullpointerexception

String.split在使用点时返回null

我有这个简单的代码: String ip = “1.2.3.4”; String[] ipArray = ip.split(“.”); System.out.println(ipArray[1]); 并且当它命中System.out.println时, ipArray为null(抛出空指针exception)。 我的问题是为什么ipArray保持null,即使我将它设置为拆分每个ip的.s?

在jar文件中包含一个文本文件并将其读取

可能重复: Java资源作为文件 我是Java的新手,我试图在Jar文件中获取一个文本文件。 在我执行我的jar时,我必须将我的文本文件放在与jar文件相同的文件夹中。 如果文本文件不存在,我将得到一个NullPointerException ,我想避免。 我想要做的是在jar中获取txt文件,所以我不会遇到这个问题。 我尝试了一些指南,但它们似乎没有用。 我目前的读取function如下: public static HashSet readDictionary() { HashSet toRet = new HashSet(); try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(“Dictionary.txt”); try (DataInputStream in = new DataInputStream(fstream)) { BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File […]

在Java中捕获nullpointerexception

我尝试使用try-catch块来捕获NullPointerException但仍然是以下程序给出错误。 我做错了什么或者是否有其他方法可以在以下程序中捕获NullPointerException 。 任何帮助都非常感谢。 public class Circular_or_not { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { LinkedListNode[] nodes = new LinkedListNode[10]; for (int i = 0; i 0 ? nodes[i – 1] : null); } // Create loop; // nodes[9].next = nodes[3]; Boolean abc= Check_Circular(nodes[0]); […]

为什么Java编译器允许通过null对象进行静态变量访问?

我指着一些技巧并遇到了这个。 在以下代码中: public class TestClass1 { static int a = 10; public static void main(String ar[]){ TestClass1 t1 = null ; System.out.println(t1.a); // At this line } } t1对象为null 。 为什么这段代码没有抛出NullPointerException ? 我知道这不是访问static变量的正确方法,但问题是关于NullPointerException 。

检查“get”调用链是否为null

假设我想执行以下命令: house.getFloor(0).getWall(WEST).getDoor().getDoorknob(); 为了避免NullPointerException,我必须在以下情况下执行以下操作: if (house != null && house.getFloor(0) && house.getFloor(0).getWall(WEST) != null && house.getFloor(0).getWall(WEST).getDoor() != null) … 是否有一种方法或已经存在的Utils类更优雅地执行此操作,让我们说下面的内容? checkForNull(house.getFloor(0).getWall(WEST).getDoor().getDoorknob());

Java:取消装箱整数时的空指针exception?

此代码导致空指针exception。 我不知道为什么: private void setSiblings(PhylogenyTree node, Color color) throws InvalidCellNumberException { PhylogenyTree parent = node.getParent(); for (PhylogenyTree sibling : parent.getChildren()) { if (! sibling.equals(node)) { Animal animal = sibling.getAnimal(); BiMap inverse = cellInfo.inverse(); int cell = inverse.get(animal); // null pointer exception here setCellColor(cell, color); } } } 我在调试器中检查了它,并且所有局部变量都是非空的。 怎么会发生这种情况? BiMap来自Google Collections。

Spring Boot – 环境@Autowired抛出NullPointerException

我使用Spring Boot 0.5.0.M5进行项目设置。 在其中一个配置文件中,我尝试使用@Autowire Environment但是因为NullPointerException而失败。 这是我到目前为止所拥有的: Application.java @EnableAutoConfiguration @Configuration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } JpaConfig.java我正在尝试@Autowire Environment @Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = “com.ui.persistence.repository”) public class JpaConfig { private static final String DATABASE_DRIVER = “db.driver”; private static final String DATABASE_PASSWORD = “db.password”; private static final String DATABASE_URL = […]

如何在getter链中跟踪NullPointerException

如果我在这样的调用中得到NullPointerException: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); 我得到一个相当无用的exception文本,如: Exception in thread “main” java.lang.NullPointerException at package.SomeClass.someMethod(SomeClass.java:12) 我发现很难找到实际上调用returend null,经常发现自己将代码重构为这样的东西: Foo ret1 = someObject.getSomething(); Bar ret2 = ret1.getSomethingElse(); Baz ret3 = ret2.getAnotherThing(); Bam ret4 = ret3.getYetAnotherOject(); int ret5 = ret4.getValue(); 然后等待更具描述性的NullPointerException,告诉我要查找哪一行。 你们中的一些人可能会认为,连接getter是一种糟糕的风格,无论如何都应该避免,但我的问题是:我可以在不更改代码的情况下找到错误吗? 提示:我正在使用eclipse,我知道调试器是什么,但我不知道如何将它应用于问题。 我对答案的结论是: 一些答案告诉我,我不应该一个接一个地连接吸气剂,一些答案显示我如何调试我的代码,如果我不赞成这个建议。 我已经接受了一个答案,它教会了我什么时候连锁吸气剂: 如果他们不能返回null,只要你喜欢就链接它们。 无需检查!= null,无需担心NullPointerExceptions(请注意链接仍然是demeter的法则,但我可以忍受 ) 如果它们可能返回null,则不要永远不会链接它们,并对每个可能返回null的值执行空值检查 这使得对实际调试的任何好建议毫无用处。

NullPointerException,在三元表达式中具有自动装箱function

运行以下Java代码: boolean b = false; Double d1 = 0d; Double d2 = null; Double d = b ? d1.doubleValue() : d2; 为什么会出现NullPointerException?

如果捕获空指针exception不是一个好习惯,捕获exception是一个好的吗?

我听说捕获NullPointerException是一种不好的做法,我认为这是明智的。 让NullPointerException传播到顶部将允许检测出错的东西。 但很多时候我看到很多朋友直接捕获Exception ,因此他们不必担心上面代码中可能出现的所有不同类型的exception。 这是一个好习惯吗? 什么是最好的未处理的其他类型的例外? 除此之外,我还可以在特定代码上处理NullPointerException ,我们确定exception的来源。 那么何时处理exception以及什么时候不应该处理它们? 什么是可能的最好的例外列表,最好不做处理?