Java NullpointerException 和过时的方法
我完全从我的并行编程书中复制了这段代码。当我尝试编译它时,我遇到了一个 nullpointerException,这似乎发生在代码中:t[i]=newThread(counters[i]); 根据 Eclipse,这是一个过时的方法。
我所做的唯一修改是添加一个 try{}catch{} 来捕获空指针异常,以允许程序实际运行。
有谁确切知道出了什么问题/如何解决它。 预先感谢
代码
import java.util.*;
import java.util.concurrent.*;
public class CountThrees implements Runnable
{
private static final int ARRAY_LENGTH=100000;
private static final int MAX_THREADS=10;
private static final int MAX_RANGE=100;
private static final Random random=new Random();
private static int count=0;
private static Object lock=new Object();
private static int[] array;
private static Thread[] t;
public static void main(String[] args)
{
array=new int[ARRAY_LENGTH];
//initialize elements in the array
for(int i=0;i<array.length;i++)
{
array[i]=random.nextInt(MAX_RANGE);
}
//create the threads
CountThrees[] counters=new CountThrees[MAX_THREADS];
int lengthPerThread=ARRAY_LENGTH/MAX_THREADS;
for(int i=0; i<counters.length; i++)
{
counters[i]=new CountThrees(i*lengthPerThread,lengthPerThread);
}
//run the threads
for(int i=0;i<counters.length; i++)
{
try
{
t[i]=new Thread(counters[i]); //NullPointerException Happens here
t[i].start();
}
catch(NullPointerException d)
{
System.out.println("Null Pointer Exception Happened");
}
}
for(int i=0;i<counters.length;i++)
{
try
{
t[i].join();
}
catch(InterruptedException e)
{}
catch(NullPointerException f)
{
System.out.println("Null Pointer Exception Happened");
}
}
//print the number of threes
System.out.println("Number of threes: " + count);
}
private int startIndex;
private int elements;
private int myCount=0;
public CountThrees(int start,int elem)
{
startIndex=start;
elements=elem;
}
//Overload of run method in the Thread class
public void run()
{
for(int i=0;i<elements; i++)
{
if(array[startIndex+i]==3)
{
myCount++;
}
}
synchronized(lock)
{
count+=myCount;
}
}
}
I copied this code exactly out of my parallel programming book. When I tried compiling it I go a nullpointerexception which seems to be happening at the code: t[i]=newThread(counters[i]); which according to the Eclipse is an obsolete method.
The only modification I made was adding a try{}catch{} to catch nullpointerexceptions to allow the program to actually run.
Does anyone know exactly what is going wrong/how to fix it.
Thanks in advance
CODE
import java.util.*;
import java.util.concurrent.*;
public class CountThrees implements Runnable
{
private static final int ARRAY_LENGTH=100000;
private static final int MAX_THREADS=10;
private static final int MAX_RANGE=100;
private static final Random random=new Random();
private static int count=0;
private static Object lock=new Object();
private static int[] array;
private static Thread[] t;
public static void main(String[] args)
{
array=new int[ARRAY_LENGTH];
//initialize elements in the array
for(int i=0;i<array.length;i++)
{
array[i]=random.nextInt(MAX_RANGE);
}
//create the threads
CountThrees[] counters=new CountThrees[MAX_THREADS];
int lengthPerThread=ARRAY_LENGTH/MAX_THREADS;
for(int i=0; i<counters.length; i++)
{
counters[i]=new CountThrees(i*lengthPerThread,lengthPerThread);
}
//run the threads
for(int i=0;i<counters.length; i++)
{
try
{
t[i]=new Thread(counters[i]); //NullPointerException Happens here
t[i].start();
}
catch(NullPointerException d)
{
System.out.println("Null Pointer Exception Happened");
}
}
for(int i=0;i<counters.length;i++)
{
try
{
t[i].join();
}
catch(InterruptedException e)
{}
catch(NullPointerException f)
{
System.out.println("Null Pointer Exception Happened");
}
}
//print the number of threes
System.out.println("Number of threes: " + count);
}
private int startIndex;
private int elements;
private int myCount=0;
public CountThrees(int start,int elem)
{
startIndex=start;
elements=elem;
}
//Overload of run method in the Thread class
public void run()
{
for(int i=0;i<elements; i++)
{
if(array[startIndex+i]==3)
{
myCount++;
}
}
synchronized(lock)
{
count+=myCount;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你永远不会分配数组 t。您收到的空指针异常是因为您的 t 数组为空。您需要将其添加到 main 方法的开头:
You never allocate the array t. The null pointer exception you're getting is because your t array is null. You need to add this right at the beginning of your main method:
我浏览了一下,但我认为问题是在尝试将数据分配给 t 之前,您没有为 t 分配内存。 Java中的数组就是指针。你正在尝试做的事情就像在 C 中一样:
这将抛出 NullPointerException。
I skimmed it but I think the problem is that you are not allocating memory for t before trying to assign data to t. Arrays in Java are pointers. What you are trying to do is like this in C:
Which will throw a NullPointerException.