InvocationTargetException

package zad31;

import javax.swing.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.*;

import java.lang.reflect.*;

public class Exec1 extends JFrame implements ActionListener {

int k = 0;

int n = 15;

JTextArea ta = new JTextArea(40,20);

List<Future> taskList= new ArrayList<Future>();

Exec1() {

add(new JScrollPane(ta));

JPanel p = new JPanel();

JButton b = new JButton("Start1");

b.setActionCommand("Start1");

b.addActionListener(this);

p.add(b);

b = new JButton("Start2");

b.setActionCommand("Start2");

b.addActionListener(this);

p.add(b);

b = new JButton("Start3");

b.setActionCommand("Start3");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop1");

b.setActionCommand("Stop1");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop2");

b.setActionCommand("Stop2");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop3");

b.setActionCommand("Stop3");

b.addActionListener(this);

p.add(b);

b = new JButton("Result1");

b.setActionCommand("Result1");

b.addActionListener(this);

p.add(b);

b = new JButton("Result2");

b.setActionCommand("Result2");

b.addActionListener(this);

p.add(b);

b = new JButton("Result3");

b.setActionCommand("Result3");

b.addActionListener(this);

p.add(b);

b = new JButton("Shutdown");

b.addActionListener(this);

p.add(b);

b = new JButton("ShutdownNow");

b.addActionListener(this);

p.add(b);

add(p, "South");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

try {

Method m = this.getClass().getDeclaredMethod("task"+cmd);

m.invoke(this);

} catch(Exception exc) { exc.printStackTrace(); }

}

class SumTask implements Callable<Integer> {

private int taskNum,

limit;

public SumTask(int taskNum, int limit) {

this.taskNum = taskNum;

this.limit = limit;

}

public Integer call() throws Exception {

int sum = 0;

for (int i = 1; i <= limit; i++) {

if (Thread.currentThread().isInterrupted()) return null;

sum+=i;

ta.append("Task " + taskNum + " part result = " + sum + '\n');

Thread.sleep(1000);

}

return sum;

}

};

Future<Integer> task;

//ExecutorService exec = Executors.newSingleThreadExecutor();

ExecutorService exec = Executors.newFixedThreadPool(3);

public void taskStart1() {

try {

taskList.add(task);

task = exec.submit(new SumTask(++k, 15));

} catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskStart2() {

try {

taskList.add(task);

task = exec.submit(new SumTask(++k, 15));

} catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskStart3() {

try {

taskList.add(task);

task = exec.submit(new SumTask(++k, 15));

} catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskResult1() {

task=taskList.get(0);

String msg = "";

if (task.isCancelled()) msg = "Task cancelled.";

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

} catch(Exception exc) {

msg = exc.getMessage();

}

}

else msg = "Task is running or waiting for execution";

JOptionPane.showMessageDialog(null, msg);

}

public void taskResult2() {

task=taskList.get(1);

String msg = "";

if (task.isCancelled()) msg = "Task cancelled.";

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

} catch(Exception exc) {

msg = exc.getMessage();

}

}

else msg = "Task is running or waiting for execution";

JOptionPane.showMessageDialog(null, msg);

}

public void taskResult3() {

task=taskList.get(2);

String msg = "";

if (task.isCancelled()) msg = "Task cancelled.";

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

} catch(Exception exc) {

msg = exc.getMessage();

}

}

else msg = "Task is running or waiting for execution";

JOptionPane.showMessageDialog(null, msg);

}

public void taskStop1() {

task=taskList.get(0);

task.cancel(true);

ta.append("Task 1 stopped.");

}

public void taskStop2() {

task=taskList.get(1);

task.cancel(true);

ta.append("Task 2 stopped.");

}

public void taskStop3() {

task=taskList.get(2);

task.cancel(true);

ta.append("Task 3 stopped.");

}

public void taskShutdown() {

exec.shutdown();

ta.append("Executor shutdown\n");

}

public void taskShutdownNow() {

List<Runnable> awaiting = exec.shutdownNow();

ta.append("Executor shutdown now - awaiting tasks:\n");

for (Runnable r : awaiting) {

ta.append(r.getClass().getName()+'\n');

}

}

public static void main(String[] args) {

new Exec1();

}

}

i get InvocationTargetException and java.lang.NullPointerException when i click eg button stop1 why it doesnt find the task eg task=taskList.get(0);

[5980 byte] By [oll3ia] at [2007-11-27 2:41:37]
# 1

You are adding a null to the list.

public void taskStart1() {

try {

taskList.add(task);// task is null at this point

hiwaa at 2007-7-12 3:05:35 > top of Java-index,Core,Core APIs...
# 2

Here's a hastily debugged version:

import javax.swing.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.*;

import java.lang.reflect.*;

public class Exec1 extends JFrame implements ActionListener {

int k = 0;

int n = 15;

JTextArea ta = new JTextArea(40,20);

List<Future><Integer>> taskList= new ArrayList<Future><Integer>>();

Exec1() {

add(new JScrollPane(ta));

JPanel p = new JPanel();

JButton b = new JButton("Start1");

b.setActionCommand("Start1");

b.addActionListener(this);

p.add(b);

b = new JButton("Start2");

b.setActionCommand("Start2");

b.addActionListener(this);

p.add(b);

b = new JButton("Start3");

b.setActionCommand("Start3");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop1");

b.setActionCommand("Stop1");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop2");

b.setActionCommand("Stop2");

b.addActionListener(this);

p.add(b);

b = new JButton("Stop3");

b.setActionCommand("Stop3");

b.addActionListener(this);

p.add(b);

b = new JButton("Result1");

b.setActionCommand("Result1");

b.addActionListener(this);

p.add(b);

b = new JButton("Result2");

b.setActionCommand("Result2");

b.addActionListener(this);

p.add(b);

b = new JButton("Result3");

b.setActionCommand("Result3");

b.addActionListener(this);

p.add(b);

b = new JButton("Shutdown");

b.addActionListener(this);

p.add(b);

b = new JButton("ShutdownNow");

b.addActionListener(this);

p.add(b);

add(p, "South");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

try {

Method m = this.getClass().getDeclaredMethod("task" + cmd);

m.invoke(this);

}

catch(Exception exc) {

exc.printStackTrace();

}

}

class SumTask implements Callable<Integer> {

private int taskNum, limit;

public SumTask(int taskNum, int limit) {

this.taskNum = taskNum;

this.limit = limit;

}

public Integer call() throws Exception {

int sum = 0;

for (int i = 1; i <= limit; i++) {

if (Thread.currentThread().isInterrupted()){

return null;

}

sum += i;

ta.append("Task " + taskNum + " part result = " + sum + '\n');

Thread.sleep(1000);

}

return sum;

}

}

Future<Integer> task;

ExecutorService exec = Executors.newFixedThreadPool(3);

public void taskStart1() {

try {

taskList.add(exec.submit(new SumTask(++k, 15)));

}

catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskStart2() {

try {

taskList.add(exec.submit(new SumTask(++k, 15)));

}

catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskStart3() {

try {

taskList.add(exec.submit(new SumTask(++k, 15)));

}

catch(RejectedExecutionException exc) {

ta.append("Execution rejected\n");

return;

}

ta.append("Task " + k + " submitted\n");

}

public void taskResult1() {

task = taskList.get(0);

String msg = "";

if (task.isCancelled()){

msg = "Task cancelled.";

}

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

}

catch(Exception exc) {

msg = exc.getMessage();

}

}

else{

msg = "Task is running or waiting for execution";

}

JOptionPane.showMessageDialog(null, msg);

}

public void taskResult2() {

task = taskList.get(1);

String msg = "";

if (task.isCancelled()){

msg = "Task cancelled.";

}

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

}

catch(Exception exc) {

msg = exc.getMessage();

}

}

else{

msg = "Task is running or waiting for execution";

}

JOptionPane.showMessageDialog(null, msg);

}

public void taskResult3() {

task = taskList.get(2);

String msg = "";

if (task.isCancelled()){

msg = "Task cancelled.";

}

else if (task.isDone()) {

try {

msg = "Ready. Result = " + task.get();

}

catch(Exception exc) {

msg = exc.getMessage();

}

}

else{

msg = "Task is running or waiting for execution";

}

JOptionPane.showMessageDialog(null, msg);

}

public void taskStop1() {

task = taskList.get(0);

task.cancel(true);

ta.append("Task 1 stopped.");

}

public void taskStop2() {

task = taskList.get(1);

task.cancel(true);

ta.append("Task 2 stopped.");

}

public void taskStop3() {

task = taskList.get(2);

task.cancel(true);

ta.append("Task 3 stopped.");

}

public void taskShutdown() {

exec.shutdown();

ta.append("Executor shutdown\n");

}

public void taskShutdownNow() {

List<Runnable> awaiting = exec.shutdownNow();

ta.append("Executor shutdown now - awaiting tasks:\n");

for (Runnable r : awaiting) {

ta.append(r.getClass().getName()+'\n');

}

}

public static void main(String[] args) {

new Exec1();

}

}

hiwaa at 2007-7-12 3:05:35 > top of Java-index,Core,Core APIs...
# 3
thank you
oll3ia at 2007-7-12 3:05:35 > top of Java-index,Core,Core APIs...