473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Getting data out of a thread... how?

blazedaces
284 100+
Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(variables needed) and then after it runs (after you setVisible(true)) you can call something to get the information that was stored inside like this: fd.getDirectory()...

So I tried something like that here:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.         final TagChoosingWindow TCW = new TagChoosingWindow(temp);
  6.         //Schedule a job for the event-dispatching thread:
  7.         //creating and showing this application's GUI.
  8.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  9.             public void run() {
  10.                 TCW.createAndShowGUI(); 
  11.             }
  12.         });
  13.         Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
  14.     }
  15.  
but this spits out a NullPointerException error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...

So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.

What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.


Thanks for all your help
-blazed
Jul 24 '07 #1
7 2117
JosAH
11,448 Expert 8TB
Alright, so I racked my brain over this problem and thought maybe I could work with it kind of like FileDialog seems to work in that you create a new FileDialog(variables needed) and then after it runs (after you setVisible(true)) you can call something to get the information that was stored inside like this: fd.getDirectory()...

So I tried something like that here:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.         final TagChoosingWindow TCW = new TagChoosingWindow(temp);
  6.         //Schedule a job for the event-dispatching thread:
  7.         //creating and showing this application's GUI.
  8.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  9.             public void run() {
  10.                 TCW.createAndShowGUI(); 
  11.             }
  12.         });
  13.         Utilities.print(TCW.getAL()); //Should print the arraylist to the screen one component at a time
  14.     }
  15.  
but this spits out a NullPointerException error because main obviously doesn't wait for the thread to finish before trying to access TCW.getAL()...

So how do I get around this? This GUI is supposed to finish when you press the done button and I hope I can then just call a method .getAL() and get my ArrayList when it's done.

What do you guys suggest? I've had zero experience with threading before so this simple concept of getting information out of a thread is eluding me. I want to avoid having the thread do some large operation when it's finished.


Thanks for all your help
-blazed
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.

kind regards,

Jos
Jul 24 '07 #2
blazedaces
284 100+
Your (main) thread can 'join' the other thread: when it stops, your thread continues
and you can grab the ArrayList from the other object.

kind regards,

Jos
Unfortunately I'm still making TCW final which means it can not change right? Yet I can't access a non-final variable inside the run() area... ? I'll try using join() ... thank you.

-blazed
Jul 24 '07 #3
blazedaces
284 100+
Alright, so my program has been modifed, I now have a class which extends thread that is as follows (very simple):

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class runTCW extends Thread { 
  4.     TagChoosingWindow TCW;
  5.     public runTCW(ArrayList<String> al) {
  6.         TCW = new TagChoosingWindow(al);
  7.     }
  8.  
  9.     public void run() {
  10.         TCW.createAndShowGUI();
  11.     }
  12.  
  13.     public TagChoosingWindow getTCW() { return this.TCW; }
  14. }
  15.  
And my main thread now looks like this:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.  
  6.         runTCW rTCW = new runTCW(temp);
  7.         rTCW.start();
  8.         try {
  9.             rTCW.join();
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.  
  14.         Utilities.print(rTCW.getTCW().getNames());
  15.     }
  16.  
It's STILL throwing a nullPointerException on that line that says "Utilities.print(rTCW.getTCW().getNames());"

I'm wondering why this could be... after all it makes sense that the thread has finished. It could be a certain line of code where I do frame.dispose()... but I thought that the variables not associated with the GUI would disappear, but not the arraylist I may have had stored somewhere.

Again thanks for the help,

-blazed

P.S.

The nullPointerException is thrown before the thread finishes so that brings me to the conclusion that I'm closing the thread incorrectly or join() is not working! Excuse my lack of experience, I'm sure I'm doing something wrong...

I'm trying to look up examples of others using join() but it's not going well...
Jul 24 '07 #4
blazedaces
284 100+
I tried to add a .isAlive() printed afterwards to see if the join() is working... well, obviously it is:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String args[]) {
  2.         ArrayList<String> temp = new ArrayList<String>(2);
  3.         temp.add("First Tag\\");
  4.         temp.add("Second Tag\\");
  5.  
  6.         runTCW rTCW = new runTCW(temp);
  7.         rTCW.start();
  8.         try {
  9.             rTCW.join();
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.  
  14.         System.out.println(rTCW.isAlive());
  15.         Utilities.print(rTCW.getTCW().getNames());
  16.     }
  17.  
Prints the following:

Expand|Select|Wrap|Line Numbers
  1. false
  2. Exception in thread "main" java.lang.NullPointerException
  3.     at AsafUtil.Utilities.print(Utilities.java:139)
  4.     at TagChoosingWindow.main(TagChoosingWindow.java:116)
  5.  
  6. Process completed.
  7.  
So clearly I'm doing something else wrong... It must have to do with my createAndShowGui method right? Is there some way to cause the thread to keep running until the frame is closed?

Thanks for your help again,

-blazed
Jul 24 '07 #5
blazedaces
284 100+
Alright, so I basically fixed my problem, but if someone who knows what they're doing reads this maybe you can tell me if there's a more conventional solution out there. This is what my createAndShowGUI() method now looks like:

Expand|Select|Wrap|Line Numbers
  1. public void createAndShowGUI() {
  2.         //Create and set up the window.
  3.         frame = new JFrame("TableDemo");
  4.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //Don't ask me why a space appears between the C and LOSE
  5.         frame.setContentPane(this);
  6.  
  7.         //Display the window.
  8.         frame.pack();
  9.         frame.setVisible(true);
  10.  
  11.         while(frame.isVisible()) { } //THIS IS THE LINE I ADDED
  12.     }
  13.  
Jul 24 '07 #6
JosAH
11,448 Expert 8TB
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.

kind regards,

Jos
Jul 25 '07 #7
blazedaces
284 100+
Shouldn't you use a modal JDialog instead? It takes care of suspending
other threads.

kind regards,

Jos
This is a much better solution... I have to finish this asap, but I will come back and make it a JDialog later...

Thank you for the help,

-blazed
Jul 25 '07 #8

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

Similar topics

11
by: Vinod I | last post by:
Hi Team, I am having a string as "System.Data.SqlDbType.Int". Now I want to convert this string type to actual type to use with my Command object Parameter Creation. How I will convert this...
9
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug...
3
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a...
14
by: Ankit Aneja | last post by:
The code of classes given below is for server to which clients connect i want to get ip address of client which has connected pls help how can i get //listen class public class listen {
1
by: Fred B | last post by:
I am launching a new thread from my application's main process (using VB.net 2003), and I can't get the child to receive the parameter I'm attempting to send it in a named data slot. The code...
5
by: Angus | last post by:
Hello I am doing this in a worker thread: char* szPartial = new char; if (szPartial) { lstrcpy(szPartial, szBuffer); PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial); }
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
1
by: Tenowg | last post by:
Hey everyone... Firstly let me tell you this is a project that is probably alittle over my head... I have played with programming most of my life, but I have only been doing VB 2005 for about 2...
3
by: Sami Vaisanen | last post by:
Hello group, I'm writing a C++ based application that embeds the python engine. Now I have a problem regarding exception/error information. Is there a way to get the exception message and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.