I am getting a null value when i am trying to access a string defined in the superclass from the sublass
my code is:
p开发者_JAVA百科ublic class Register_window extends javax.swing.JFrame
{
String u;
.
.
.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
.
.
u=jTextField.getText();
.
.
}
}
}
public class Data_storer extends Register_window
{
public Vector people_database() throws Exception
{
.
.
.
System.out.println("The name of the user as printed in Data_storer(inherited from Register_window) is:" + u);
.
.
.
}
}
You need to initialize u. You're making the assumption that jButton1ActionPerformed
is called before people_database
.
Are you sure you're actually assigning anything to u
? If you never click jbutton1 (and thus jButton1ActionPerformed
isn't called) then u
really will be null
.
Your problem is akin to this:
public class FooA {
public static void main(String[] args) {
A a = new A();
a.setFieldA("bar");
B b = new B();
System.out.println(b.getFieldA()); // this will still print "foo"
}
}
class A {
String fieldA = "foo";
public void setFieldA(String a) {
this.fieldA = a;
}
public String getFieldA() {
return fieldA;
}
}
class B extends A {
}
If you make changes to fieldA in object a, it will not effect object b since they both carry distinct fieldA's. The way to solve this is to not extend A, but construct your second class like class C, to use a reference to the current A object and then extract necessary information from the A object as needed:
class C {
private A a;
C(A a) {
this.a = a;
}
public String getAFieldA() {
return a.getFieldA();
}
}
Then in the main you could do:
public class FooA {
public static void main(String[] args) {
A a = new A();
a.setFieldA("bar");
B b = new B();
System.out.println(b.getFieldA()); // this will still print "foo"
C c = new C(a);
System.out.println(c.getAFieldA()); // this will print "bar"
}
}
Translated into your code, it would look something like so:
public class Register_window extends javax.swing.JFrame {
private JTextField jTextField = new JTextField();
// String u; // this is not necessary
// ...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// ...
// somehow notify Data_storer that a field has changed.
// ...
} finally {
}
}
public String getTextFieldText() {
return jTextField.getText();
}
}
And the Data_storer class:
public class Data_storer extends Register_window {
private Register_window registerWindow;
// pass the current visualized Register_window instance into constructor
public Data_storer(Register_window register_window) {
this.registerWindow = register_window;
}
public Vector people_database() throws Exception {
// ...
System.out.println("The name of the user as printed in Data_storer(inherited from Register_window) is:"
+ registerWindow.getTextFieldText());
// ...
}
}
Another possible solution is to turn things around, to have your GUI hold an instance of the data storer, and have it "push" information into the data storer object on button push, and this actually makes more sense to me. For instance:
public class Register_window extends javax.swing.JFrame {
private JTextField jTextField = new JTextField();
private Data_storer dataStorer = new Data_storer();
// ...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// ...
Vector vect = dataStorer.people_database(jTextField.getText());
// do something with the vect here
}
//...
}
}
class Data_storer extends Register_window {
public Vector people_database(String text) {
//...
}
}
精彩评论