473,666 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InvocationTarge tException

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 InvocationTarge tException and java.lang.NullP ointerException when i click eg button stop1 why it doesnt find the task eg task=taskList.g et(0);
Apr 27 '07 #1
0 1077

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

Similar topics

1
11040
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 or any other Excel extras. However, now is the time to get the complicated ones to work. I didn't think that there would be a difference, but there is. At first, I was beginning to think that it was a file size problem, but now I am unsure. ...
0
2294
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 oracle.sql.NUMBER._fromLnxFmt(NUMBER.java:3181) at oracle.sql.NUMBER.toBigDecimal(NUMBER.java:636) at oracle.jdbc.dbaccess.DBConversion.NumberBytesToBigDecimal(DBConversio n.java:2805) at oracle.jdbc.driver.OracleStatement.getBigDecimalValue(OracleStatement ..java:4539)
5
5504
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 = datas.get(rowToDelete); String idName = idField.getColumnName(); Method method = dataObjectClass.getMethod("get" +
0
1939
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" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <!--The component and its script are in the xalan namespace and define the
1
2305
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 past the following error. Any help, please? =========================================================================== C:\xalan>test C:\xalan>REM @echo off
0
9372
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 are using connection pool to connect to db2. Database product name : DB2/AIX64 Database product version : SQL08025 JDBC driver name : IBM DB2 JDBC Universal Driver Architecture JDBC driver version : 2.9.31
0
7075
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. (Source code below) package oracle.forms.demos.ole; import com.jacob.activeX.ActiveXComponent; import java.awt.Panel; import com.jacob.com.*;
2
2960
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 work!!! Somebody knows why?
9
43657
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 scientific programming languages without any problem..
0
8360
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8784
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8642
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2774
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 we have to send another system
2
1777
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.