LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-12-2009, 09:34 AM   #1
jmite
Member
 
Registered: Jun 2008
Distribution: Ubuntu
Posts: 266

Rep: Reputation: 36
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.
 
Old 11-13-2009, 03:43 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by jmite View Post
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
 
Old 11-13-2009, 06:40 AM   #3
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
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()
 
  


Reply

Tags
java, netbeans, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Objects needs imports?! manolakis Programming 1 05-16-2009 09:14 PM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
java objects zaicheke Programming 3 03-23-2005 06:21 AM
java arrays and objects titanium_geek Programming 13 03-07-2004 01:21 PM
Objects in Java? mikeshn Programming 0 06-11-2002 10:21 AM


All times are GMT -5. The time now is 07:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration