-2
import java.io.*;
class threads extends Thread{
    int counter=0;
    static volatile boolean counting=true;
    public void run(){
        if(counting) {
            counter++;
        }
    }
}
public class cores {
    static PrintWriter pw= new PrintWriter(System.out, true);
    @SuppressWarnings({ "static-access", "deprecation" })
    public static void main(String args[]) throws InterruptedException {
        int mastercounter=0;
        Thread mainthread=Thread.currentThread();
        int cores= Runtime.getRuntime().availableProcessors();      
        threads[] array=new threads[cores];
        for (int x=0;x<cores;x++) {
            threads t=array[x];
            t.start();
        }
        mainthread.sleep(5000);
        threads.counting=false;
        for (int x=0;x<cores;x++) {
            threads t=array[x];
            t.stop();
            mastercounter+=t.counter;
        }
        pw.println(mastercounter);
    }

The code above is my attempt at a simple benchmark program that tries to take advantage of all cores available on the CPU of a computer by simply using all the threads possible to count in 5 seconds.
The code gives a NullPointerException on the line t.start() which I assume is because of the way I tried to work around not being able to name variables dynamically.
Any advice, fixes or tips are much appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
D. Kay
  • 1
  • @Hover Should probably be "how to initialize an array of objects" – OneCricketeer Jan 01 '18 at 20:09
  • @cricket_007: I'll ask SOCVR to close this question in a few days – Hovercraft Full Of Eels Jan 01 '18 at 20:17
  • @HovercraftFullOfEels Oh! The Miolnir! The Miolnir! So, isn't this just another NPE? Which you wouldn't consider a duplicate? The Miolnir! The Miolnir! Incoherence made human. – Phantômaxx Jan 01 '18 at 21:57
  • @NoiseGenerator: This exactly illustrates my point.. Notice that the NPE Q/A used to close this question was not the usual NPE question but rather one more specific to NPE and reference arrays. Always better to close with a more specific duplicate if possible. – Hovercraft Full Of Eels Jan 01 '18 at 22:01
  • @HovercraftFullOfEels NPEs are NPEs. That's my point of view. The more generic, the best. Since you will learn to handle a wider range of them – Phantômaxx Jan 02 '18 at 00:40

2 Answers2

0
threads[] array=new threads[cores];

You create new array of type threads which is never assigned any value, as threads is a class the array itself is an array that holds references. Default value for the array of references is null, when you call:

t.start();

you are invoking start() method on a null value, therefore you get NullPointerException. You need to initialize your array before assigning values in it and calling methods on those values.

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
0

Your array has no elements. threads t=array[x]; will of course be null.

Try to make some objects, and please capitalize your classes

array[x] = new Threads();
array[x].start();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245