Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Based on the question, i want to have the output is shown based on the second method used using getter. but the error shown : null pointer.

Note: i already try it by using the "Static" in variable.

So, is there any other way to calling the getter without use "static" ?

means, in this below method part :
Java
public void secondMethod_getTheName(){
System.out.println("Name: "+theModel.getName());}


What I have tried:

model class
Java
package com.SetterAndGetter;

public class model {

	private  int id;
	private  String name;
	private  int Age;

	public  int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public  String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public  int getAge() {
		return Age;
	}

	public void setAge(int age) {
		Age = age;
	}

	@Override
	public String toString() {
		return "model [id=" + id + ", name=" + name + ", Age=" + Age + "]";
	}

}


myMethod Class
Java
package com.SetterAndGetter;

public class myMethod {

	public model theModel;

	public void setTheName() {
		String name = "mike";

		theModel = new model();
		theModel.setName(name);
	}

	public void getTheName() {

		System.out.println("The name is: " + theModel.getName());
	}

}


Main Class
Java
package com.SetterAndGetter;


public class mainApp {
	private model theModel;


	public void secondMethod_getTheName() {

		System.out.println("Name: "+theModel.getName());


	}

	public static void main(String[] args) {

		myMethod obj1 = new myMethod();
		obj1.setTheName();
		obj1.getTheName();

		mainApp obj2 = new mainApp();
		obj2.secondMethod_getTheName();

	}
}
Posted
Updated 27-Mar-18 21:21pm

Quote:
System.out.println("Name: "+theModel.getName());

You are using an object without having instantiated it. You have to create the object (and set its name), e.g.
Java
mainApp obj2 = new mainApp();
obj2.theModel = new model();
obj2.theModel.setName("foo");
obj2.secondMethod_getTheName();
 
Share this answer
 
Comments
Asyraf Patt 27-Mar-18 4:45am    
public void secondMethod_getTheName() {

System.out.println("Name: "+theModel.getName());
}

How should i print mike, by using the same getter ?
CPallini 27-Mar-18 6:15am    
You cannot. You have to create an instance of myMethod (as you have already done) in order to get it (alternatively you could replace "foo", with "mike" in my code).
Your model class is almost fine (it lacks a proper ctor, in my opinion) but it looks you don't master how to access it.
Maciej Los 27-Mar-18 6:45am    
5ed!
CPallini 27-Mar-18 11:30am    
Thank you very much.
First Solution

Mymethod class
package com.SetterAndGetter;

public class myMethod {

	public model theModel;

	public static  String theName;

	public void setTheName(String tempName) {
		theName = "john";
		tempName = theName;


		theModel = new model();
		theModel.setName(theName);
	}

	public void getTheName() {

		System.out.println("The name is: " + theModel.getName());

	}

}


Model Class
package com.SetterAndGetter;

public class model {

	private int id;
	private  String name;
	private  int Age;



	public  int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public  String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public  int getAge() {
		return Age;
	}

	public void setAge(int age) {
		Age = age;
	}

	@Override
	public String toString() {
		return "model [id=" + id + ", name=" + name + ", Age=" + Age + "]";
	}

}


Main Method
package com.SetterAndGetter;

public class mainApp {
	private myMethod mm;

	public void secondMethod_getTheName() {

		System.out.println("the second method: "+myMethod.theName);

	}

	public static void main(String[] args) {

		myMethod obj1 = new myMethod();
		obj1.setTheName(null);
		obj1.getTheName();

		mainApp obj2 = new mainApp();
		obj2.secondMethod_getTheName();


	}
}


Second Solution
by using the inheritance

my method class
package com.SetterAndGetter;

public class myMethod {

	/**
	 * Create new object of the model Class
	 */
	public static model Model = new model();

	private String tempName;

	public myMethod() {
	}

	public myMethod(String stempName) {

		// reference the variable as tempName == stempName
		this.tempName = stempName;

		//define value of stempName as mike
		stempName = "mike";

		//Set the name
		Model.setName(stempName);


	}

	public void dispMethod(){

		System.out.println("Use Getter: "+Model.getName());
	}

}


second method class
package com.SetterAndGetter;

/**
 * Created by Asyraf-PC on 3/28/2018.
 */

public class mySecondMethod extends myMethod {


    public void secondMethodDisplay() {

        System.out.println("Use Second Getter: "+Model.getName());

    }
}


model
package com.SetterAndGetter;

public class model {

	private int id;
	private  String name;
	private  int Age;



	public  int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public  String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public  int getAge() {
		return Age;
	}

	public void setAge(int age) {
		Age = age;
	}

	@Override
	public String toString() {
		return "model [id=" + id + ", name=" + name + ", Age=" + Age + "]";
	}

}


main App

package com.SetterAndGetter;

public class mainApp {


	public static void main(String[] args) {

	myMethod obj = new myMethod("");
	obj.dispMethod();

	mySecondMethod obj2 = new mySecondMethod();
	obj2.secondMethodDisplay();


	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900