Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:23:38+00:00 2026-05-26T04:23:38+00:00

I copied this code exactly out of my parallel programming book. When I tried

  • 0

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;
}
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:23:39+00:00Added an answer on May 26, 2026 at 4:23 am

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I copied and pasted this binary data out of sql server, which I am
I have the following code, copied exactly from a exercise from a PHP book.
I copied this code from an example . I've read it 100 times. Array.prototype.map
I have this sample code for async operations (copied from the interwebs) public class
I have the following code which i copied from here : /*************************************************************************** * _
I have this code copied from Android developers website: public class ExampleActivity extends Activity
I have an HTML file, just with the AdMob code (exactly copied from AdMob
I've just been experimenting with MPI, and copied and ran this code, taken from
I just copied this code from the MIT video lecture that is posted online:
this is copied from http://www.zenspider.com/ZSS/Products/RubyInline/Readme.html , the home of rubyinline, adding/moding as indicated in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.