0

I am a beginner of Java programming learner. I can't know how to correct the following Java program. Kindly help to let me know how to correct it. Thanks a lot.

public class TMA1Q2 {

public static void main(String[] args) {
    System.out.println("Usage: java TMA1Q2 {number of Threads}");

    // Create tasks
    Runnable taskA = new PrintTwoConcurThreads("Thread A ");
    Runnable taskB = new PrintTwoConcurThreads("            Thread B ");

    // Create threads
    Thread thread1 = new Thread(taskA);
    Thread thread2 = new Thread(taskB);

    // Start threads
    thread1.start();
    thread2.start();
}

}

// The task that implements Runnable

class PrintTwoConcurThreads implements Runnable {

private final String TwoConcurThreads;
private String[] args;

public PrintTwoConcurThreads(String numThreads) {
    TwoConcurThreads = numThreads;
}

// Override the run() method
@Override
public void run() {
    // Print the value input argument times
    int numThreads = Integer.parseInt(args[0]);
    Thread[] myThread;
    myThread = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        System.out.println(TwoConcurThreads + i);
    }
}
}
4
  • It is not clear what you're trying to do. I can see Thread[] is useless. Commented Dec 8, 2013 at 9:33
  • Kindly see the problem after debugging: Usage: java TMA1Q2 {number of Threads} Exception in thread "Thread-0" java.lang.NullPointerException at PrintTwoConcurThreads.run(TMA1Q2.java:38) at java.lang.Thread.run(Thread.java:744) Exception in thread "Thread-1" java.lang.NullPointerException at PrintTwoConcurThreads.run(TMA1Q2.java:38) at java.lang.Thread.run(Thread.java:744) BUILD SUCCESSFUL (total time: 0 seconds) Commented Dec 8, 2013 at 9:33
  • I want to write a program with two concurrent threads. The Two threads write "Thread A X" and "Thread B X" on screen respectively where X takes on successive values from 0 to n - 1 where n is the argument used to run program. Thank you. Commented Dec 8, 2013 at 9:36
  • 1
    Hi @user3079494, edit the question; include the error that you receive and what you are trying to achieve. That will make it easier for people to help you and will make the question more useful for future readers. Commented Dec 8, 2013 at 9:39

1 Answer 1

1
private String[] args;

This args field is never initialized, so it will be having a default value of null.

When you try to access it in following line you get a NullPointerException

int numThreads = Integer.parseInt(args[0]);

It is not clear what you're trying to do. Atleast this would help you to see what's going wrong.

Also I have no Idea why following lines are used, You create Thread[] but never you used it.

Thread[] myThread;
myThread = new Thread[numThreads];
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, I have also no idea because I am a fresh learner and just read from a reference book to try. Thanks.
Actually, I try to write a program with two concurrent threads. The two threads write "Thread A X" and "Thread B X" on screen respectively where X takes on successive values from 0 to n - 1 where n is the argument used to run program. Thank you so much.
The output screen likes below:
Thread A 0 Thread B 0 Thread A 1 Thread A 2 Thread B 1 Thread A 3 Thread B 2 Thread A 4 Thread B 3 Thread B 4 Thread A 5 Thread B 5
Please try the following program. It can works but I don't know how to the argument used to run the program.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.