accessing textfields

J

J. Albers

Heya,

I want to write into a textfield from another class, but it keeps giving me
the error: C:\Documents and Settings\jalbers\My Documents\Visual Studio
Projects\XML Fusion\XMLread.java(104): Cannot access non-static field
'Form1.listBox1' from a static context

Does anyone know what this means and how to solve it?

Grtz, Joachim.
 
R

Ryan Stewart

J. Albers said:
Heya,

I want to write into a textfield from another class, but it keeps giving me
the error: C:\Documents and Settings\jalbers\My Documents\Visual Studio
Projects\XML Fusion\XMLread.java(104): Cannot access non-static field
'Form1.listBox1' from a static context

Does anyone know what this means and how to solve it?

Grtz, Joachim.

It means just what it says. You can't access a non-static anything from a
static anything. i.e.:

public class Something {
int x;
public static void main(String [] args) {
x = 5;
}
}

doesn't work because x belongs to an instance of Something and main doesn't.
In short, x doesn't exist until you create an instance of Something. This
will work:

public class Something {
int x;
public static void main(String [] args) {
Something s = new Something();
s.x = 5;
}
}

As will this:

public class Something {
static int x;
public static void main(String [] args) {
x = 5;
}
}

They do two very different things, though. If you don't know the difference,
I'd suggest you turn to the early pages of a Java book and read carefully.
 
J

J. Albers

But i made nothing static :(

Ryan Stewart said:
J. Albers said:
Heya,

I want to write into a textfield from another class, but it keeps giving me
the error: C:\Documents and Settings\jalbers\My Documents\Visual Studio
Projects\XML Fusion\XMLread.java(104): Cannot access non-static field
'Form1.listBox1' from a static context

Does anyone know what this means and how to solve it?

Grtz, Joachim.

It means just what it says. You can't access a non-static anything from a
static anything. i.e.:

public class Something {
int x;
public static void main(String [] args) {
x = 5;
}
}

doesn't work because x belongs to an instance of Something and main doesn't.
In short, x doesn't exist until you create an instance of Something. This
will work:

public class Something {
int x;
public static void main(String [] args) {
Something s = new Something();
s.x = 5;
}
}

As will this:

public class Something {
static int x;
public static void main(String [] args) {
x = 5;
}
}

They do two very different things, though. If you don't know the difference,
I'd suggest you turn to the early pages of a Java book and read carefully.
 
C

Christophe Vanfleteren

J. Albers wrote:

But i made nothing static :(

You'll need to show your code if you want to get this solved. You certainly
are accessing an instance variable from a static context.
 
O

Oscar Kind

Note: I reordered the post to reflect the quoting habits of this
newsgroup.

J. Albers said:
Ryan Stewart said:
It means just what it says. You can't access a non-static anything from a
static anything. i.e.:
[...]

But i made nothing static :(

Check again. Something at line 104 of XMLread.java is static.

Is you use the SUN naming conventions, my guesss is that Form1 is a class
name. This means that Form1.listBox1 must be static as well. Either make
it static, or use an instance of Form1. The second option is probably
best, but you'll have to decide that yourself.


Oscar
 
J

J. Albers

Check again. Something at line 104 of XMLread.java is static.

Is you use the SUN naming conventions, my guesss is that Form1 is a class
name. This means that Form1.listBox1 must be static as well. Either make
it static, or use an instance of Form1. The second option is probably
best, but you'll have to decide that yourself.


Oscar


Cheers, that did the trick :)
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top