Java NullpointerException 和过时的方法

发布于 2024-12-09 17:37:29 字数 2074 浏览 1 评论 0原文

我完全从我的并行编程书中复制了这段代码。当我尝试编译它时,我遇到了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-12-16 17:37:29

你永远不会分配数组 t。您收到的空指针异常是因为您的 t 数组为空。您需要将其添加到 main 方法的开头:

t = new Thread[MAX_THREADS];

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 = new Thread[MAX_THREADS];
四叶草在未来唯美盛开 2024-12-16 17:37:29

我浏览了一下,但我认为问题是在尝试将数据分配给 t 之前,您没有为 t 分配内存。 Java中的数组就是指针。你正在尝试做的事情就像在 C 中一样:

static thread* t;
//Need to initialize t here using t = new t[MAX_THREADS]
t[i] = ... 

这将抛出 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:

static thread* t;
//Need to initialize t here using t = new t[MAX_THREADS]
t[i] = ... 

Which will throw a NullPointerException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文