-1

I'm getting an error even though I don't see the problem by reviewing the code.

It says the error is at "char[] linechars;".

I'm not sure why I'm getting this error when using character arrays in the Employee1 class.

public class Employee1 {
    public String name;
    public String number;

    Scanner keyboard = new Scanner(System.in);

    char[] lineChars;

    public Employee1() {
        this.lineChars = number.toCharArray();
    }

    public void setName() {
        System.out.println("enter name");
        name = keyboard.nextLine();
    }

    public String getName() {
        System.out.println("your name is " + name);
        return name;
    }

    public void setNumber() {
        System.out.println("enter number");
        for (int i = 0; i < lineChars.length; i++) {
            if (i == 0 || i == 1 || 1 == 2 || i > 0 && i <= 9) {
                number = keyboard.nextLine();
            } else {
                System.out.println("must be 1-9");
            }
        }
    }

    public String getNumber() {
        System.out.println("your number: ");
        return number;
    }
}
Shaun Wilson
  • 8,727
  • 3
  • 50
  • 48
  • In the `Employee1` constructor, what is the value of `number`? – 001 Oct 19 '17 at 19:52
  • 2
    If you're getting an unwanted and unexpected `NullPointerException`, then certainly there ***is*** at least one error in your code. – John Bollinger Oct 19 '17 at 19:53
  • @JohnnyMopp I originally had it as char [] lineChars = number.toCharArray(); but i was getting the same problem – Storm_troopa Oct 19 '17 at 19:57
  • @JohnBollinger I really dont have one error just the when i run Main.java in the terminal. – Storm_troopa Oct 19 '17 at 19:58
  • 1
    @Storm_troopa That's because `number` is `null` in the ctor. You need to set it to something before trying to call `toCharArray()`. Also, does the `ProductionWorker1` class derive from the `Employee1` class? – 001 Oct 19 '17 at 20:02
  • I see, @Storm_troopa. We are having a minor terminology issue. That your code exhibits no *compilation* errors (or even no compilation warnings) does not mean it is error-free. Would that it did, but the compiler is just not that smart. Indeed, a `NullPointerException` in your program can happen only at run time, so if you get one then it can be taken for granted that your program compiles. – John Bollinger Oct 19 '17 at 20:10
  • public String number; <-- not initialized, it defined as null. Then when you use this.lineChars = number.toCharArray(); since number is null you get npr – logger Oct 19 '17 at 20:20
  • Please provide the source code for `ProductionWorker1` and the full exception stack trace. I am unable to reproduce the problem with an assumed `ProductionWorker1` implementation and either `1` or `2` as the program input. – Mark A. Fitzgerald Oct 19 '17 at 20:43

1 Answers1

1

Welcome to StackOverflow. First, some pointers to help get a better response in the future:

  1. Please run your code through a formatter/beautifier so that others can more easily read your code. For example, TutorialsPoint.com has a decent formatter. This code was virtually unreadable.
  2. When discussing an error please provide the full, exact error being seen. A proper error detail as reported by the JRE will not only get you a faster response and more accurate answers, your question is less likely to get down-voted due to poor quality.
  3. Do not paste "all your code", if members need more they will ask for it. Instead, include only the class that is identified in the error detail reported by the JRE.

Now, the problem explained:

When we create a new object instance the code in the Constructor is executed.

In your case, when you create a new Employee1 instance this line of code is executed:

this.lineChars = number.toCharArray();

Since number is null (has not been assigned a value), you get an exception.

Consider the following:

public Employee1()
{   
    number = "12345";
    this.lineChars = number.toCharArray();
}

This is just an example to illustrate the problem with your code. Probably you would want to pass a value into the constructor instead of hard-coding the value:

public Employee1(String someNumber)
{   
    number = someNumber;
    this.lineChars = number.toCharArray();
}

// somewhere else in your program
var employee = new Employee1("12345");

Hope this helps you solve your problem, for more information on NullPointException and why they occur see this answer.

Shaun Wilson
  • 8,727
  • 3
  • 50
  • 48
Jay Wilson
  • 66
  • 5