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 7727347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:26:13+00:00 2026-06-01T05:26:13+00:00

I have a simple exercise where I call a Thread and subsequently the run()

  • 0

I have a simple exercise where I call a Thread and subsequently the run() method.

I do however get a NullPointerException.

Struggling to figure out why this exception is there.

package practice;
import java.util.Date;
import java.text.SimpleDateFormat;


class IdDisplay implements Runnable
{
    String idNumber;
    IdDisplay(String ID)
    {
        this.idNumber = ID;

    }
    char [] idMine = this.idNumber.toCharArray();
    public void run()
    {
        for(int i = 0; i < idMine.length; i++)
        {
            System.out.print(idMine[i] + " ");
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException in){}


        }
    }       
}
public class Practice
{
    public static void main(String[]args)
    {


        String name = "Arian";
        String age = "38";
        String id = "7401195021087";

        int ageInt;
        int year;
        int yearBorn;

        System.out.println("Welcome " + name + " your age is " + age + " and your ID number " + id);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();

        year = Integer.parseInt(sdf.format(date));
        ageInt = Integer.parseInt(age);
        yearBorn = year - ageInt;
        System.out.println("You were born in " + yearBorn);

        IdDisplay idClass = new IdDisplay(id);
    Thread tt = new Thread(idClass);
    tt.start(); 



    }
}   

Why do I get this NullPointerException.

Regards

  • 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-01T05:26:14+00:00Added an answer on June 1, 2026 at 5:26 am

    You have two instance fields declared:

    String idNumber;
    

    and

    char [] idMine = this.idNumber.toCharArray();
    

    The initializer of the second one assumes the first one is not null, which it is to start with, hence the exception. Instance initializers are processed prior to entry to the constructor.

    Instead, move that initialization into the constructor:

    IdDisplay(String ID)
    {
        this.idNumber = ID;
        this.idMine = this.idNumber.toCharArray();
    }
    

    Also strongly recommend declaring all of your fields in one place (I prefer them at the beginning of the class, but where you put them is a matter of style; that you put them together is a more substantial maintenance consideration). (Caveat to the general rule: There is an argument, in the case of fields backing accessors, for declaring the field next to the accessor.) And some vertical whitespace between things can be useful.

    The complete updated IdDisplay class:

    class IdDisplay implements Runnable
    {
        String  idNumber;
        char [] idMine;
    
        IdDisplay(String ID)
        {
            this.idNumber = ID;
            this.idMine = this.idNumber.toCharArray();
        }
    
        public void run()
        {
            for(int i = 0; i < idMine.length; i++)
            {
                System.out.print(idMine[i] + " ");
                try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException in){}
            }
        }       
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, This is just a simple exercise from class that I decided to have
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have set up this programming exercise. using System; using System.Collections.Generic; using System.Linq; using
This seems like a simple exercise, but I'll be darned if I can find
I have what I think is a simple solr exercise, but I'm unsure what
I am new to Objective-C and as a very simple learning exercise, I have
I have a simple ASP.NET site making a call to a WCF service. The
I have simple regex \.*\ for me its says select everything between and ,
I have simple win service, that executes few tasks periodically. How should I pass
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How

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.