Object not instantiated (not previously done a new)

0

I have the following:

public class Person{

private String name;
private double age;

public Person() throws Exception{       
    setName("Dummy");
    age= 12;    
}

public Person(double age) {
    this.age= age;
}

public String getName() {
        return name;
    }

public void setName(String name) throws Exception{
        if(name.length()>15){
            throw new Exception("The name cannot be longer than 15 characters!!");
        }
        this.name = name;
}   

public void setValue(double value) {
        this.value= value;
}

And as a main program I have:

public class PersonCheck{

    public static void main(String[] args){
        Person person= null;
        boolean exit = false;
        int operation = 0;

        do{ 
            try{                
                 operation = in.nextInt();

                 switch(operation){
                        System.out.println("Person's name is  "+artwork.getName());
                        break;
            }catch(NullPointerException e2){//Error que se lanza cuando se intenta acceder a un objeto que no ha sido instanciado (i.e. no se ha hecho previamente "new").
        System.err.println("[ERROR] You have to create an person first!!");

The question I have is that I constantly get the error that I did not do the instantiated and I do not know how to do it. Help?

I have put the pieces of the code that I think are the important ones. If you need more, tell me about it.

    
asked by jdk 12.10.2018 в 12:14
source

1 answer

3

your first line of the main is:

Person person= null;

Change it to:

Person person= new Person();

It is necessary to build an object with "new" before accessing it.

    
answered by 12.10.2018 / 12:44
source