accessing textfields

Discussion in 'Java' started by J. Albers, Jan 16, 2004.

  1. J. Albers

    J. Albers Guest

    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.
    J. Albers, Jan 16, 2004
    #1
    1. Advertising

  2. J. Albers

    Ryan Stewart Guest

    "J. Albers" <> wrote in message
    news:bu8bnj$aca$...
    > 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.
    Ryan Stewart, Jan 16, 2004
    #2
    1. Advertising

  3. J. Albers

    J. Albers Guest

    But i made nothing static :(

    "Ryan Stewart" <> wrote in message
    news:...
    > "J. Albers" <> wrote in message
    > news:bu8bnj$aca$...
    > > 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. Albers, Jan 19, 2004
    #3
  4. 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
    Christophe Vanfleteren, Jan 19, 2004
    #4
  5. J. Albers

    Oscar Kind Guest

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

    J. Albers <> wrote:
    > "Ryan Stewart" <> wrote in message
    > news:...
    >> "J. Albers" <> wrote in message
    >> news:bu8bnj$aca$...
    >> > 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.
    Oscar Kind, Jan 19, 2004
    #5
  6. J. Albers

    J. Albers Guest


    > > 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!
    J. Albers, Jan 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=

    How to format "Drop Down List" TextFields and ValueFields?

    =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=, Jul 4, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    13,165
    Eliyahu Goldin
    Jul 4, 2005
  2. Jim
    Replies:
    1
    Views:
    423
    Andrew Thompson
    Apr 10, 2004
  3. SteKel2002
    Replies:
    0
    Views:
    428
    SteKel2002
    Dec 21, 2003
  4. lrantisi
    Replies:
    2
    Views:
    330
    crazzybugger
    Nov 26, 2006
  5. Markus
    Replies:
    1
    Views:
    116
Loading...

Share This Page