Arianule
Arianule

Reputation: 9043

NullPointerException with creating a thread

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

Upvotes: 1

Views: 172

Answers (7)

sulai
sulai

Reputation: 5354

Your null pointer exception most likely comes from this line:

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

This is because you initialize this.idNumber in the constructor. However, idMine will be initialized earlier, at a time where this.idNumber is still null.

Upvotes: 1

Suresh
Suresh

Reputation: 1504

You have created two instance So it will be give null pointer exception. Actually in your code what happend char array variable intialize before you called constructor so it take always value null.

Try below one.

class IdDisplay implements Runnable
{
    String idnumber;
    char[] idMine;
    public  IdDisplay(String ID)
    {
        idnumber=ID;
        idMine = 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(); 
    }
}   

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074295

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){}
        }
    }       
}

Upvotes: 4

giorashc
giorashc

Reputation: 13713

move this line :

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

to the constructor (after you set idNumber). In its current place it is initialized when you create the instance of IdDisplay and since idNumber is not initialized you will get an exception.

Upvotes: 1

Alex Stybaev
Alex Stybaev

Reputation: 4693

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

Tries to intialize before the

String idNumber;

And gives you an NPE.

Upvotes: 1

JTeagle
JTeagle

Reputation: 2196

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

This declaration is at class scope, and at that point this.idNumber is a string object that hasn't been allocated. Pop it into your run() method.

Upvotes: 1

Tudor
Tudor

Reputation: 62439

The problem is here:

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

This line executes before the constructor is called, in which you are initializing this.idNumber.

Suggested change:

String idNumber;
char [] idMine;

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

Upvotes: 1

Related Questions