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

  • Home
  • SEARCH
  • 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 6240005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:29:25+00:00 2026-05-24T11:29:25+00:00

How can I access class level variables inside thread, I have tried to access

  • 0

How can I access class level variables inside thread, I have tried to access variable directly but the exception occur. Is it possible to access class level variable inside thread or I have to use handler?

Here is my Code.

public class MainMapActivity extends MapActivity 
        {    
            //***************************************************************************************
            //isRouteDisplayed Method (Override)
            @Override
            protected boolean isRouteDisplayed() 
            {
                return false;
            }//End method
            //***************************************************************************************
            //private Handler handlerTimer = new Handler();

            //***************************************************************************************
            //Class Level Variables & Objects

            String[] Device_ID = null;

            //***************************************************************************************
            //onCreate method (Override the built-in method) Called when the activity is first created.
            @Override
            public void onCreate(Bundle savedInstanceState) 
            {

                //handlerTimer.removeCallbacks(taskUpdateStuffOnDialog);
                //handlerTimer.postDelayed(taskUpdateStuffOnDialog , 100); 


               //service = new NewService();

               new Thread(taskUpdateStuffOnDialog).start(); 
                // GIve the Server some time for startup   
                try 
                {  
                       Thread.sleep(500);  
                } 
                catch (InterruptedException e)
                { 

                }  

               // Kickoff the Client  
               // startService(new Intent(this, NewService.class));
                new Thread(new NewService()).start(); 



            }//End onCreate 
            //***************************************************************************************

            private Runnable taskUpdateStuffOnDialog = new Runnable() 
            {

                public void run() 
                {  
                    GlobalVariable appState = ((GlobalVariable)getApplicationContext());
                    try
                    {
                         serverAddr = InetAddress.getByName(SERVERIP);  
                         Log.d("UDP", "S: Connecting...");  
                        /* Create new UDP-Socket */  
                         socket = new DatagramSocket(SERVERPORT, serverAddr);  

                    }
                    catch (Exception e)
                    {
                        Log.e("UDP", "S: Error", e);  

                    }

                    while(true)
                    {
                            // TODO Auto-generated method stub  
                            try 
                            {  
                                         /* Retrieve the ServerName */  
                                         /* By magic we know, how much data will be waiting for us */  
                                         byte[] buf = new byte[72];  
                                         /* Prepare a UDP-Packet that can 
                                         * contain the data we want to receive */ 

                                         DatagramPacket packet = new DatagramPacket(buf, buf.length);  
                                         Log.d("UDP", "S: Receiving...");  


                                         /* Receive the UDP-Packet */  
                                         socket.receive(packet);

                                         String id = new String(packet.getData());

                                         //buffer.order(ByteOrder.LITTLE_ENDIAN);  // if you want little-endian



                                        String[] tokens = id.split(" ");
                                         String b = tokens[0];

                                         Log.d("Message:   ", b + " "  );
                                /***************************** sending  device ID *************************************/


                                else if(b.equals("5"))
                                {
                                    Device_ID[0] = tokens[1];

                                    runOnUiThread(new Runnable()
                                    {
                                        public void run() 
                                        {

                                            String id = tokens[1];
                                            Log.d("UDP", "S: Received Device ID: '" + id + "'");


                                            setID(id);
                                            //positionOverlay.setID(id);
                                            //addEvent(id);
                                            Toast.makeText(MainMapActivity.this, "Your Device ID " + id,Toast.LENGTH_LONG).show();
                                        }
                                    });
                                    //Toast.makeText(MainMapActivity.this, "Your Device ID " + b,Toast.LENGTH_LONG).show();

                                } // end else if


                           } // end try
                            catch (Exception e)
                           {  
                                Log.e("UDP", "S: Error", e);  
                           } // end catch  

                            //handlerTimer.postDelayed(this, 100);
                      } // end while condition  
                    //***************************************************************************************
                  }//End Run method
              //***************************************************************************************
            };//End Thread
            //***************************************************************************************




}

And exception is null pointer exception at Device_ID[0] = tokens[1];
this line.

  • 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-05-24T11:29:25+00:00Added an answer on May 24, 2026 at 11:29 am

    That doesn’t look like syntactically valid Java to me. You should have seen a compilation error telling you that ClassToken = token; in not recognized. And if it had gotten past that, another one saying that you haven’t declared the run() method. And there’s a missing semicolon.


    If you would care to post the real code that is giving the error, we might be able to tell you what the real problem is.


    The problem is that you have initialized Device_ID to null rather than an array. When you try to index a null you will get an NPE.

    The simple fix is to change the initialization to:

    String[] Device_ID = new String[1];
    

    (From your code as given, I can’t see why you need to use an array at all. Why not just make Device_ID a String? OTOH, perhaps this is just an artefact of your demonstration code …)

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

Sidebar

Related Questions

I have a domain class containing a couple of fields. I can access them
How can I access a variable in one public class from another public class
How can I access a static variable in a separate class in PHP? Is
How can I access Class object for Generics? Currently, I am doing it this
How can hibernate can access a private field/method of a java class , for
Protected Means, we can access this member only in a deriving class, and internal
How can I access a public static member of a Java class from ColdFusion?
How can I access dynamically properties from a generated LINQ class ? Cause I
I would like to access a class everywhere in my application, how can I
(programming in iPhone objective-C) I have a class level NSString* that I create, add

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.