Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > accessing textfields

Reply
Thread Tools

accessing textfields

 
 
J. Albers
Guest
Posts: n/a
 
      01-16-2004
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.


 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      01-16-2004
"J. Albers" <(E-Mail Removed)> wrote in message
news:bu8bnj$aca$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
J. Albers
Guest
Posts: n/a
 
      01-19-2004
But i made nothing static

"Ryan Stewart" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "J. Albers" <(E-Mail Removed)> wrote in message
> news:bu8bnj$aca$(E-Mail Removed)...
> > 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.
>
>



 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      01-19-2004
J. Albers wrote:

<snip explanation on static vs instance methods>
>>
>>

> 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.

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
Oscar Kind
Guest
Posts: n/a
 
      01-19-2004
Note: I reordered the post to reflect the quoting habits of this
newsgroup.

J. Albers <(E-Mail Removed)> wrote:
> "Ryan Stewart" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "J. Albers" <(E-Mail Removed)> wrote in message
>> news:bu8bnj$aca$(E-Mail Removed)...
>> > 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

>>
>> 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

--
No trees were harmed in creating this message.
However, a large number of electrons were terribly inconvenienced.
 
Reply With Quote
 
J. Albers
Guest
Posts: n/a
 
      01-20-2004

> > 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



Cheers, that did the trick
Thanks!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with spaces in textfields. lrantisi Java 2 11-26-2006 03:44 PM
How to format "Drop Down List" TextFields and ValueFields? =?Utf-8?B?Y2hhcmxpZXdlc3Q=?= ASP .Net 4 07-04-2005 04:16 PM
How to layout textfields and keep size at 1 character height Jim Java 1 04-10-2004 07:08 PM
how to keep state of checkboxes and textfields SteKel2002 HTML 0 12-21-2003 02:10 PM
querying textfields outside of forms... Markus Javascript 1 09-19-2003 01:36 PM



Advertisments