简体   繁体   English

Java:尝试将对象添加到BlockingQueue时出现NullPointerException吗?

[英]Java: NullPointerException when trying to add object to BlockingQueue?

I found a similar question about a PriorityQueue, the error with that one was that it wasn't initialized correctly. 我发现了一个关于PriorityQueue的类似问题,该错误是未正确初始化。 I might have the same problem, but i can't figure out how to initialize it correctly! 我可能有同样的问题,但是我不知道如何正确初始化它!

As of now i just do: 到目前为止,我只是这样做:

BlockingQueue myQueue = null; BlockingQueue myQueue = null;

but that throws an exception as soon as i try to add something to the list. 但这会在我尝试向列表中添加内容时引发异常。

How do i correctly initialize a BlockingQueue? 如何正确初始化BlockingQueue?

BlockingQueue<E> is an interface. BlockingQueue<E>是一个接口。 You need to pick a specific implementation of that interface, such as ArrayBlockingQueue<E> , and invoke one of its constructors like so: 您需要选择该接口的特定实现,例如ArrayBlockingQueue<E> ,并按如下所示调用其构造函数之一:

BlockingQueue<E> myQueue = new ArrayBlockingQueue<E>(20);

If you're unsure what different types of blocking queues exist in the JDK, look under "All Known Implementing Classes" . 如果不确定JDK中存在哪些不同类型的阻塞队列,请查看“所有已知的实现类”下的内容

If you call any method on null you will get a null pointer exception. 如果在null上调用任何方法,则将获得null指针异常。 Try making a new ArrayBlockingQueue, which implements the interface. 尝试制作一个新的ArrayBlockingQueue来实现该接口。

Please read the javadocs which also has examples http://download.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html 请阅读javadocs,其中也包含示例http://download.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html

BlockingQueue blockingQueue = new ArrayBlockingQueue(100); BlockingQueueblockingQueue =新的ArrayBlockingQueue(100); // there are other implementations as well, in particular that uses a linked list and scales better than the array one. //还有其他实现,尤其是使用链表并且比数组更好地扩展。

  1. Make BlockingQueue hold a certain type, for example BlockingQueue<String> or something similar. 使BlockingQueue保持某种类型,例如BlockingQueue<String>或类似的东西。
  2. You need to initialize the variable with an implementation of BlockingQueue , for example ArrayBlockingQueue<E> . 您需要使用BlockingQueue的实现来初始化变量,例如ArrayBlockingQueue<E>

So do something like: 因此,请执行以下操作:

BlockingQueue<MyObject> = new ArrayBlockingQueue<MyObject>();

and you'll be fine. 你会没事的。

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

相关问题 java:尝试添加到对象列表并获取nullPointerException - java: Trying to add to an object list and getting nullPointerException 尝试将对象添加到PriorityQueue时出现NullPointerException - NullPointerException when trying to add an object to a PriorityQueue 尝试向JTextPane添加撤消时出现java.lang.NullPointerException - java.lang.NullPointerException when trying to add undos to JTextPane 尝试将连接对象传递给另一个Java类时发生NullPointerException - NullPointerException when trying to pass connection object to another java class java NullPointerException尝试绘制时 - java NullPointerException when trying to draw Java Object(Boolean,Integer)添加时获取NullPointerException - Java Object(Boolean,Integer) when add get NullPointerException 尝试调用对象的函数时,java中的NullPointerException - NullPointerException in java while trying to call a function of an object 尝试以编程方式将TableRow添加到TableLayout时发生NullPointerException - NullPointerException when trying to add a TableRow to a TableLayout programmatically 尝试添加导航抽屉时出现NullPointerException(Android) - NullPointerException when trying to add a Navigation Drawer (Android) 尝试添加多项时遇到NullPointerException - Encountering NullPointerException when trying to add polynoms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM