I am getting the following error in my Java program:
Java variables not initialized error… error : variable nam and r not
initialized location class child
But nan and r already initialized, yet I am still getting the same error.
public class cla {
int rn;
String name;
void get(String a, int x) {
name = a;
rn = x;
}
void display() {
System.out.println("student name: " + name + "roll no.:" + rn);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class child {
public static void main(String args[]) throws IOException {
String nam;
int r;
BufferedReader bReader = new BufferedReader(new InputStreamReader(
System.in));
try {
System.out.println("enter name and roll no.");
nam = bReader.readLine();
r = Integer.parseInt(bReader.readLine());
} catch (Exception e) {
}
cla c = new cla();
c.get(nam, r);
c.display();
}
Local variablesdon’t get default values, you should initialize them before you use them , initializenamandrwith default values inside your main and you will be fine.BTW, consider naming your class’s and variables something meaningful.