简体   繁体   English

空指针异常修复 - 列表

[英]Null Pointer Exception Fix - Lists

  public void createGraph () {
    int oldFrom = -1;
    int oldTo = -1;
    for(int i = 0; i < edges.size(); i++) {
      EdgeI e = edges.get(i);
      int from = e.from;
      int to = e.to;
      VertexI v = vertices.get(from);
      if (from == oldFrom && to == oldTo){
        vertexWeight wic = v.neighbors.get(v.neighbors.size() - 1);
        wic.w++;
      }
      else {
        v.neighbors.add(new vertexWeight (to, 1));
        oldFrom = from;
        oldTo = to;
      }
    }
  }

neighbors is a public List from VertexI class. neighborsVertexI类的公共List w is a public integer from vertexWeight class. wvertexWeight类的公共整数。 edges is a list located in my main class. edges是位于我的主类中的列表。 I keep getting a null pointer exception for this line of code: 我一直得到这行代码的空指针异常:

v.neighbors.add(new vertexWeight (to, 1));

Tried working on it for around 15 minutes and I didn't get it to work. 尝试工作大约15分钟,我没有让它工作。 What am I messing up on? 我搞砸了什么?

java.lang.NullPointerException
    at tester.createGraph(tester.java:60)
    at tester.main(tester.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Short answer 简短的回答

Initialize v.neighbors with new ArrayList() in vertices.get() . 初始化v.neighborsnew ArrayList()vertices.get()

Long answer 答案很长

Your question omitted a crucial information: How you initialized neighbors . 您的问题省略了一个重要信息:您如何初始化neighbors Why is this important? 为什么这很重要?

See: What is a NullPointerException, and how do I fix it? 请参阅: 什么是NullPointerException,以及如何解决它?

In your case I guessed that either v or neighbors is null during the run of the program. 在你的情况下,我猜测在程序运行期间vneighbors是null。 For example vertices.get(from) could return null and v.neighbors won't work. 例如, vertices.get(from)可以返回null,而v.neighbors不起作用。 Or neighbors is null, and v.neighbors.add() won't work. 或者neighbors为null, v.neighbors.add()将不起作用。

And voilà. 瞧。 You admitted that you set neighbors to null when initializing VertexI . 您承认在初始化VertexI时将neighbors设置为null。

The solution is: Initialize with new ArrayList() instead of null . 解决方案是:使用new ArrayList()而不是null初始化。

If that would not have been possible or you cannot avoid null pointers for some other reason, you can do null pointer checks like this: 如果那是不可能的,或者由于某些其他原因你无法避免空指针,你可以像这样做空指针检查:

if (v != null && v.neighbors != null) {
    v.neighbors.add(new vertexWeight (to, 1));
}

This means, don't add vertices if v or neighbors are null. 这意味着,如果vneighbors为空,请不要添加顶点。

But this is complicated and error-prone. 但这很复杂且容易出错。 It is easier to avoid null pointers as much as possible. 尽可能避免使用空指针更容易。 Some would say, avoid them at all costs! 有人会说,不惜一切代价避免它们! Throw an exception instead or return an "empty" object like new ArrayList() . 改为抛出异常或返回像new ArrayList()这样的“空”对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM