Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:05:37+00:00 2026-06-14T03:05:37+00:00

In the following Java code I am populating an ArrayList in one class then

  • 0

In the following Java code I am populating an ArrayList in one class then using the ArrayList in another class. However, the array transfers but is filled with null values. I have debugged the code and the ArrayList does get populated but its values do not transfer.

First Class:

public String[] log = new String[100];
public ArrayList llog= new ArrayList();
int arraypos= 0;
public void nativeKeyPressed(NativeKeyEvent e) {
  if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) {
    GlobalScreen.unregisterNativeHook();
  }
  System.out.println(" " + NativeKeyEvent.getKeyText(e.getKeyCode()).toLowerCase() + " ");
  llog.add(NativeKeyEvent.getKeyText(e.getKeyCode()).toString().toLowerCase());
  System.out.println("list:" + llog.get(arraypos));
  arraypos = arraypos + 1;  
}

public ArrayList getStrokes (){
  return this.llog;
}

Second Class:

public void TeslaTimer()
{
  int delay = 10000; //1 sec = 1000 
  int period = 90000; // repeat every sec.

  Timer timer = new Timer();
  timer.scheduleAtFixedRate(new TimerTask()
  {
    @Override
    public void run()
    {
      GlobalKeyListener keyl = new GlobalKeyListener();
      ArrayList keylog=keyl.getStrokes();

      //TeslaLogger savetoFile = new TeslaLogger();
      //savetoFile.TeslaLogger(keylog);
      System.out.println("Ring Ring");
      for(int i =0; i < keylog.size(); i++){
        System.out.println(keylog.get(i));
      }
    }
  }, delay, period);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:05:38+00:00Added an answer on June 14, 2026 at 3:05 am

    The instance of GlobalKeyListener in TeslaTimer is not the same instance as the one you are adding to – the are different objects each with their own list.

    You must make the same instance of GlobalKeyListener available to your run() method, or perhaps use a static variable (which is like a global variable).


    Here’s the Singleton pattern in action that you could use to make it work:

    public class GlobalKeyListener {
    
        // rest of class omitted
        private static GlobalKeyListener INSTANCE = new GlobalKeyListener();
    
        public static GlobalKeyListener getInstance() { 
            return INSTANCE;
        }
    
    }
    

    then wherever you need to use it locally:

    GlobalKeyListener globalKeyListener = GlobalKeyListener.getInstance();
    globalKeyListener.getStrokes(); // etc
    

    All code will then be using the same instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to a remote database using the following java code, but
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
Say suppose I have the following Java code. public class Example { public static
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
what should the following java code do? public class foo{ public static void main(String[]
The following Java code is a very simple piece of code, but what are
I have the following Java code: public class FirstActivity extends Activity implements OnClickListener {
I have the following Java code - import java.lang.reflect.Field; public class AnnotationTest { public
Here's my problem, I'm using the following java code using JMF for playing video.
The following Java code just parses a date (with time portion) 2009-01-28-09:11:12 using SimpleDateFormat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.