LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem accessing java objects in netbeans (http://www.linuxquestions.org/questions/programming-9/problem-accessing-java-objects-in-netbeans-768671/)

jmite 11-12-2009 09:34 AM

Problem accessing java objects in netbeans
 
So, I'm still pretty fresh at programming. In Netbeans, the name of the project is cardgame_rewrite. My main application file is such
Code:

/*
 * Cardgame_rewriteApp.java
 */

package cardgame_rewrite;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class Cardgame_rewriteApp extends SingleFrameApplication {

    /**
    * At startup create and show the main frame of the application.
    */
    @Override protected void startup() {
        show(new Cardgame_rewriteView(this));
    }

    /**
    * This method is to initialize the specified window by injecting resources.
    * Windows shown in our application come fully initialized from the GUI
    * builder, so this additional configuration is not needed.
    */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
    * A convenient static getter for the application instance.
    * @return the instance of Cardgame_rewriteApp
    */
    public static Cardgame_rewriteApp getApplication() {
        return Application.getInstance(Cardgame_rewriteApp.class);
    }

    /**
    * Main method launching the application.
    */
    public static void main(String[] args) {
        launch(Cardgame_rewriteApp.class, args);
        Player player1 = new Player();
        Player player2 = new Player();
        Turn CurrentTurn;
        CurrentTurn = new Turn();
        player1.theTurn = CurrentTurn;
        player2.theTurn = CurrentTurn;
        while (player1.life > 0 && player2.life > 0)
        {
            while (player1.movesLeft > 0)
            {
                CurrentTurn.currentPlayer = player1;
            }
            player1.movesLeft = 5;
            while (player2.movesLeft > 0)
            {
                CurrentTurn.currentPlayer = player2;
            }
            player2.movesLeft = 5;
        }

    }
}

The class Turn has an int member called direc.
As you can see above, I've declared a Turn object called CurrentTurn.

In my GUI file, I'm trying to create a method of action when the user clicks a button, the value of CurrentObject.direc is set to 12.

Code:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    CurrentTurn.direc=12;
    }

However, the program keeps giving warnings as if CurrentTurn didn't exist.

Is this a scope problem, or a problem with public/private variables? I've declared both the Turn class and its member variables as public.

JohnGraham 11-13-2009 03:43 AM

Quote:

Originally Posted by jmite (Post 3754292)
However, the program keeps giving warnings as if CurrentTurn didn't exist.

Please post specific error messages - either the first error message you get (that's the *very first*, as fixing this will often cause later errors to go away) or just cut & paste the complete output of trying to compile if you don't know what to look for.

Specific error messages are a lot easier to address - general descriptions of error messages can be very cryptic!

John G

gzunk 11-13-2009 06:40 AM

Two things jump out. One is that in Java, you would not normally capitalize the first letter in a variable name. The reason for this is that class names normally do have a capital as the first letter and if you keep the this convention it's easy to see what's a class and what's an object.

Secondly, you declare CurrentTurn as a Turn object, but you do it within the main method, which limits the scope to be only within the main method. You cannot access CurrentTurn from anywhere outside the main method.

Rather than tell you where to declare CurrentTurn, I'll just say that you need to read up on scoping and access rules. That's not me being unhelpful, it's just that depending upon where you want to access CurrentTurn from, define where you need to declare it.

For example you say "In my GUI file" which implies that the code you are writing isn't going to be defined in the Cardgame_rewriteApp, which means that putting it as a member variable of Cardgame_rewriteApp wouldn't do any good.

What *I* would do is create a member variable on Cardgame_rewriteApp, called "turn". I would then create a couple of accessors - getTurn() and setTurn(Turn turn). You should be able to get a reference to the main app from your GUI file (getApplication looks a likely candidate), in which case to get a reference to the turn you would have to do getApplication().getTurn()


All times are GMT -5. The time now is 05:29 AM.