You are accessing two different instances of ScriptVariables so they don't share instance variables. The fact that you have set the text and foodName properties on the first instance does not affect the new instance you create in your other class.
What you can do is either make the text and foodName properties static, or pass a reference to the first ScriptVariables instance to the second piece of code.
You are accessing two different instances of ScriptVariables so they don't share instance variables. The fact that you have set the text and foodName properties on the first instance does not affect the new instance you create in your other class.
What you can do is either make the text and foodName properties static, or pass a reference to the first ScriptVariables instance to the second piece of code.
ScriptVariables sv = new ScriptVariables(); is creating a different instance than the one for which you previously set its fields (foodName and amount). This new instance has its foodName and amount fields still not initialized, hence the null and zero values.
Assuming your second class is ScriptVariablesAccessor, then you can include a field of type ScriptVariables in its class definition. This field would then be used to pass the previously filled instance via a constructor or a setter method.
public class ScriptVariablesAccessor {
private ScriptVariables sv;
public ScriptVariablesAccessor(ScriptVariables sv) {
this.sv = sv;
}
// or use a setter
public void setScriptVariables(ScriptVariables sv) {
this.sv = sv;
}
public void processVariables() {
String foodName = sv.getFoodName();
int amount = sv.getAmount();
// do something with foodName and amount;
}
}
Below is how you would call this class with the prepared ScriptVariables:
ScriptVariablesAccessor svAccessor = new ScriptVariablesAccessor(sv);
svAccessor.processVariables();
If you are on IntelliJ you need to download the Lombok plugin. You can download it from Preferences -> Plugins -> Lombok

I faced the same issue, Maybe you need to add one Lombok plugin, that will fix the issue at least for me or others
Could you please refer the below pages? And follow the steps to add the Lombok plugin
Solution 1
Solution 2
Solution 3
Please refer IntelliJ
Please refer Eclipse/STS
You must tell which object the method getYear you are referring to. In your example, you must call getYear as agecalculator.getYear() and all should be fine.
try this
ageProgram.java
import java.util.Scanner;
public class ageProgram {
public static void main(String[]args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("enter your birth year");
int yearBirth = keyboard.nextInt();
System.out.println("enter a (future) year");
int yearTo = keyboard.nextInt();
ageCalculator agecalculator = new ageCalculator(yearBirth, yearTo);
if (yearBirth > yearTo) {
System.out.format("You will be born in %s years", agecalculator.getYear());
} else {
System.out.format("You are %s years old", agecalculator.getYear());
}
}
}
ageCalculator.java
public class ageCalculator {
String yearText;
public ageCalculator(int yearBirth, int yearTo) {
if (yearBirth > yearTo) {
int year = (yearBirth - yearTo);
yearText = String.valueOf(year);
} else {
int year = (yearTo - yearBirth);
yearText = String.valueOf(year);
}
}
public String getYear() {
return yearText;
}
}
if you don't understand it, i can explain
edit: i didn't get it at first but the above answer is also correct. you should use agecalculater.getYear() to call the method edit: you should start a class name with a capital letter. and you can use
yearText = String.valueOf(yearBirth - yearTo);
instead of
int year = (yearTo - yearBirth);
yearText = String.valueOf(year);
The getters are functions, not fields; so to call them you need to use () (like in: pers[i].getAdress().
Also, class names should be capitalized, but it's not really important (just makes it easier to read.
As someone suggested in a comment, you should use an IDE, in case you are not doing it. The IDE will point you to the obvious little mistakes (both errors and convention mistakes) like the ones above, and save you a long time. If you don't know where to get started for that, search Eclipse or Netbeans. (Eclipse is my personal favourite).
EDIT: I just saw Eran commented the answer to your error, so if he posts it as an answer accept his first.
in all your sysout statements you are trying to access getter without () this.
please try this
System.out.println(pers[i].getName()); //change all your getters
instead of
System.out.println(pers[i].getName);
because getName() is a method not a Variable so always you need to use open-close braces () just after the method name
(either you defining or calling) .
please change all these methods (methods which raise an compile-time Error).
System.out.println(pers[i].getName);
System.out.println(pers[i].getAddress); //all these lines are causing of Compile time error.
System.out.println(pers[i].getAge);
System.out.println(pers[i].getPhoneNumber);
In your constructor, you need to use the same tests as in getters and setters instead of setting the values directly. Currently, you circumvent the tests in the setter with new Cylinder(-1,-1).
Your constructor should call your setters, and you should check your logic in the setters. Do you really want to carry on with the value of 1 if the calling code passes a negative value?
Within the constructor of game the 'difficultLvl' member will be zero as that is what it is initialised to - no reason to expect it to be 2. Once constructed you use the setter method to set the value to 2; from then on the value will be 2 until set to something else.
If you were to add a getter method:
public int getDifficulty() {
return difficultLvl;
}
and call this you'll see the value.
I suspect you don't want to construct a new Game on every mouse click but instead keep one and just call the setter method on mouse click:
private MainGame m2 = new MainGame(this);
public Setting (JFrame owner) {
super(owner, true);
initComponents();
setSize(400, 250);
setLocation(370, 250);
getContentPane().setBackground(new Color(128, 201, 20));
}
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
difficultLvl is an instance variable, so it has a value for each instance. Every time you create a new instance of Game, it has its own diffucultLvl initialized to 0. If you set the difficultLvl in one Game, it doesn't change it for other Game instances and it doesn't affect future new Game instances.
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
MainGame m2 = new MainGame(this);
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
In this code, you create a game, MainGame m2 = new MainGame(), but it has default difficulty, which is what is printed in the constructor. Next, you set its difficulty level (if you print the difficulty after this, it will be right). Then game is thrown away: it goes out of scope - it was only a local variable.