473,507 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InvocationTargetException

oll3i
679 Contributor
Expand|Select|Wrap|Line Numbers
  1. package zad31;
  2.  
  3.  
  4.  
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.concurrent.*;
  10. import java.lang.reflect.*;
  11.  
  12. public class Exec1 extends JFrame implements ActionListener {
  13.  
  14.   int k = 0;
  15.   int n = 15;
  16.   JTextArea ta = new JTextArea(40,20);
  17.   List<Future> taskList= new ArrayList<Future>();
  18.   Exec1() {
  19.     add(new JScrollPane(ta));
  20.     JPanel p = new JPanel();
  21.     JButton b = new JButton("Start1");
  22.     b.setActionCommand("Start1");
  23.     b.addActionListener(this);
  24.     p.add(b);
  25.     b = new JButton("Start2");
  26.     b.setActionCommand("Start2");
  27.     b.addActionListener(this);
  28.     p.add(b);
  29.     b = new JButton("Start3");
  30.     b.setActionCommand("Start3");
  31.     b.addActionListener(this);
  32.     p.add(b);
  33.     b = new JButton("Stop1");
  34.     b.setActionCommand("Stop1");
  35.     b.addActionListener(this);
  36.     p.add(b);
  37.     b = new JButton("Stop2");
  38.     b.setActionCommand("Stop2");
  39.     b.addActionListener(this);
  40.     p.add(b);
  41.     b = new JButton("Stop3");
  42.     b.setActionCommand("Stop3");
  43.     b.addActionListener(this);
  44.     p.add(b);
  45.     b = new JButton("Result1");
  46.     b.setActionCommand("Result1");
  47.     b.addActionListener(this);
  48.     p.add(b);
  49.     b = new JButton("Result2");
  50.     b.setActionCommand("Result2");
  51.     b.addActionListener(this);
  52.     p.add(b);
  53.     b = new JButton("Result3");
  54.     b.setActionCommand("Result3");
  55.     b.addActionListener(this);
  56.     p.add(b);
  57.     b = new JButton("Shutdown");
  58.     b.addActionListener(this);
  59.     p.add(b);
  60.     b = new JButton("ShutdownNow");
  61.     b.addActionListener(this);
  62.     p.add(b);
  63.     add(p, "South");
  64.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.     pack();
  66.     setVisible(true);
  67.   }
  68.  
  69.   public void actionPerformed(ActionEvent e)  {
  70.     String cmd = e.getActionCommand();
  71.     try {
  72.       Method m = this.getClass().getDeclaredMethod("task"+cmd);
  73.       m.invoke(this);
  74.     } catch(Exception exc) { exc.printStackTrace(); }
  75.   }
  76.  
  77.  
  78.   class SumTask implements Callable<Integer> {
  79.  
  80.     private int taskNum,
  81.                 limit;
  82.  
  83.     public SumTask(int taskNum, int limit) {
  84.       this.taskNum = taskNum;
  85.       this.limit = limit;
  86.     }
  87.  
  88.     public Integer call() throws Exception {
  89.       int sum = 0;
  90.       for (int i = 1; i <= limit; i++) {
  91.         if (Thread.currentThread().isInterrupted()) return null;
  92.         sum+=i;
  93.         ta.append("Task " + taskNum + " part result = " + sum + '\n');
  94.         Thread.sleep(1000);
  95.       }
  96.       return sum;
  97.     }
  98.   };
  99.  
  100.   Future<Integer> task;
  101.  
  102.   //ExecutorService exec = Executors.newSingleThreadExecutor();
  103.   ExecutorService exec = Executors.newFixedThreadPool(3);
  104.  
  105.   public void taskStart1() {
  106.     try {
  107.  
  108.       taskList.add(task);
  109.       task = exec.submit(new SumTask(++k, 15));
  110.     } catch(RejectedExecutionException exc) {
  111.         ta.append("Execution rejected\n");
  112.         return;
  113.     }
  114.     ta.append("Task " + k + " submitted\n");
  115.   }
  116.  
  117.   public void taskStart2() {
  118.         try {
  119.  
  120.           taskList.add(task);
  121.           task = exec.submit(new SumTask(++k, 15));
  122.         } catch(RejectedExecutionException exc) {
  123.             ta.append("Execution rejected\n");
  124.             return;
  125.         }
  126.         ta.append("Task " + k + " submitted\n");
  127.       }
  128.  
  129.   public void taskStart3() {
  130.         try {
  131.  
  132.           taskList.add(task);
  133.           task = exec.submit(new SumTask(++k, 15));
  134.         } catch(RejectedExecutionException exc) {
  135.             ta.append("Execution rejected\n");
  136.             return;
  137.         }
  138.         ta.append("Task " + k + " submitted\n");
  139.       }
  140.   public void taskResult1() {
  141.  
  142.  
  143.         task=taskList.get(0);
  144.     String msg = "";
  145.     if (task.isCancelled()) msg = "Task cancelled.";
  146.     else if (task.isDone()) {
  147.       try {
  148.         msg = "Ready. Result = " + task.get();
  149.       } catch(Exception exc) {
  150.           msg = exc.getMessage();
  151.       }
  152.     }
  153.     else msg = "Task is running or waiting for execution";
  154.     JOptionPane.showMessageDialog(null, msg);
  155.  
  156.   }
  157.  
  158.   public void taskResult2() {
  159.  
  160.  
  161.         task=taskList.get(1);
  162.   String msg = "";
  163.   if (task.isCancelled()) msg = "Task cancelled.";
  164.   else if (task.isDone()) {
  165.     try {
  166.       msg = "Ready. Result = " + task.get();
  167.     } catch(Exception exc) {
  168.         msg = exc.getMessage();
  169.     }
  170.   }
  171.   else msg = "Task is running or waiting for execution";
  172.   JOptionPane.showMessageDialog(null, msg);
  173.  
  174. }
  175.  
  176.   public void taskResult3() {
  177.  
  178.  
  179.         task=taskList.get(2);
  180. String msg = "";
  181. if (task.isCancelled()) msg = "Task cancelled.";
  182. else if (task.isDone()) {
  183.   try {
  184.     msg = "Ready. Result = " + task.get();
  185.   } catch(Exception exc) {
  186.       msg = exc.getMessage();
  187.   }
  188. }
  189. else msg = "Task is running or waiting for execution";
  190. JOptionPane.showMessageDialog(null, msg);
  191.  
  192. }
  193.   public void taskStop1() {
  194.     task=taskList.get(0);
  195.     task.cancel(true);
  196.     ta.append("Task 1 stopped.");
  197.   }
  198.  
  199.   public void taskStop2() {
  200.     task=taskList.get(1);
  201.     task.cancel(true);
  202.     ta.append("Task 2 stopped.");
  203.   }
  204.  
  205.   public void taskStop3() {
  206.       task=taskList.get(2);
  207.     task.cancel(true);
  208.     ta.append("Task 3 stopped.");
  209.   }
  210.   public void taskShutdown() {
  211.     exec.shutdown();
  212.     ta.append("Executor shutdown\n");
  213.   }
  214.  
  215.   public void taskShutdownNow() {
  216.     List<Runnable> awaiting = exec.shutdownNow();
  217.     ta.append("Executor shutdown now - awaiting tasks:\n");
  218.     for (Runnable r : awaiting) {
  219.       ta.append(r.getClass().getName()+'\n');
  220.     }
  221.  
  222.  }
  223.  
  224.  
  225.   public static void main(String[] args) {
  226.      new Exec1();
  227.   }
  228.  
  229. }
  230.  
  231.  
  232.  
i get InvocationTargetException and java.lang.NullPointerException when i click eg button stop1 why it doesnt find the task eg task=taskList.get(0);
Apr 27 '07 #1
0 1071

Sign in to post your reply or Sign up for a free account.

Similar topics

1
11028
by: Dan | last post by:
Hello, I am trying to read and write to an Excel file via my Java applet. I have done so successfully on several simple Excel files that simply had data cells without many complicated equations...
0
2277
by: Rahul | last post by:
I am getting the following exception when accessing one of my tables. java.lang.reflect.InvocationTargetException: java.lang.ArrayIndexOutOfBoundsException at...
5
5499
by: Bernard Koninckx | last post by:
Hi everybody, The following code (putted in a inherited object from AbstractTableModel object) make some errors : public void deleteRow(int rowToDelete){ try{ Object dataObject =...
0
1926
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
1
2292
by: Fisch von Gestern | last post by:
I have tried to run the extension function/element examples provided with the Xalan-J download. I believe that my classpath is correct, and that my versions are up-to-date. However, I can't get...
0
9361
by: nleesy | last post by:
Our web application running on WebSphere hits the following two exceptions randomly (once or twice a day) while opening connection to db2. Does anyone has idea about the cause of the exception. We...
0
7071
by: anuj13singhal | last post by:
Hi, I am trying to open a word document on the client machine. the document is present on the oracle application server (on windows). For that I have created a java class file wordbean.class....
2
2949
by: inetjack | last post by:
Hi, This is a little test application, generating and compiling code at runtime. The loadClassLoader() method of the Factory Object suppose to unload all class previously loaded. It does not...
9
43521
by: krishna81m | last post by:
How to create dynamic variables as follows for(...) { Run This( "int var" + i = i;); } to be able to create and initialize var1 = 1; var2 = 2; I was able to do this in a couple of...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.