Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Nonstatic/static Referencing
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gryphon




PostPosted: Sun Jun 01, 2008 3:08 am   Post subject: Nonstatic/static Referencing

I am currently trying to make Monopoly in JCreator. I'm having a problem calling my methods and using my global variables in the main part of my program. The errors i'm getting all say: non-static method/variable (name) cannot be referenced from a static context. I have tried vigorously and without success to fix these problems, could someone please help me find a solution, any help would be greatly appreciated, thanks!
My program is over 800 lines long so i'll provide just the main program and the first method i'm having trouble with.
code:

import java.io.*;
import java.util.*;
public class MonopolyCompleteMain
{       Property[] properties = new Property[22];
        Player[] players = new Player[4];
        public static void main ()
        {       BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                String choice;
                int counter = 0;
               
                loadProps();//Problem line when loadProps() is not static
               
                for(int j=0;j<4;j++)
                {       System.out.println("Enter the name of player " + (j + 1) + ": ");
                        players[j] = new Player(get(keyboard));//Problem line
                }

                intro();
               
                while(true)
                {       Menu1();
                        choice = get(keyboard);
                        if(choice.equals("1")) //Roll the Dice
                        {       rollTheDice(counter);//Problem line
                                counter += 1;
                        }
               
                        else if(choice.equals("2"))//View All Info
                        {       viewAllInfo(counter);//Problem line
                        }
                       
                        else if(choice.equals("3"))//Buy Houses
                        {       buyHouses(counter);//Problem line
                        }
                       
                        else if(choice.equals("4"))//Buy Hotel
                        {       buyHotel(counter);//Problem line
                        }
                       
                        else if(choice.equals("5"))//Sell Houses
                        {       sellHouses(counter);//Problem line
                        }
                       
                        else if(choice.equals("6"))//Sell Hotel
                        {       sellHotel(counter);//Problem line
                        }
                        else if(choice.equals("7"))//Unmortgage Properties
                        {       unmortgagedProp(counter);//Problem line
                        }                                    
                        if(counter==5)//Sets counter to 0 when players[counter] goes past 4     
                        {       counter=0;          
                        }
                }
        }

        //Loads the properties from the .txt file
        private void loadProps()
        {       BufferedReader inFile = new BufferedReader(new InputStreamReader(System.in));
                String name;
                String strPosition;
                String strPrice;
                String strBaseRent;
                String strColour;
                int position;
                int price;
                int baseRent;
                int colour;
       
                try{    inFile = new BufferedReader(new FileReader("propertiesComplete.txt"));
                }
                catch(IOException e)
                { System.out.println("ERROR");
                }
               
                for(int i=0;i<40;i++)
                {       name = get(inFile);
                        strPosition = get(inFile);
                        strPrice = get(inFile);
                        strBaseRent = get(inFile);
                        strColour = get(inFile);
                       
                        //convert from string to int as needed here
                        position = Integer.valueOf(strPosition);
                        price = Integer.valueOf(strPrice);
                        baseRent = Integer.valueOf(strBaseRent);
                        colour = Integer.valueOf(strColour);
       
                        properties[i] = new Property(name,position,price,colour,baseRent);//Problem line when loadProps() is static
                }
        }


By the way, I'm finding that if i try to make loadProps() static then the compiler still has a problem with the properties variable within the loadProps method, if i had to guess i'd say that the global variables are the problem.
Sponsor
Sponsor
Sponsor
sponsor
Reality Check




PostPosted: Sun Jun 01, 2008 8:29 am   Post subject: Re: Nonstatic/static Referencing

You are trying to reference a non-static variable within your static method. Putting a 'static' in front of your global varialbes should fix these problems.
wtd




PostPosted: Sun Jun 01, 2008 11:25 am   Post subject: RE:Nonstatic/static Referencing

Static and non-static contexts are covered rather early in my Introduction to Java. Read it. Smile
Reality Check




PostPosted: Sun Jun 01, 2008 3:04 pm   Post subject: Re: Nonstatic/static Referencing

And yes I would definitely suggest going through wtd's intro to java. You will get a grasp of all the basics including static and non-static methods.
Gryphon




PostPosted: Sun Jun 01, 2008 3:36 pm   Post subject: Re: Nonstatic/static Referencing

Thanks for the help, wtd your guide really helped me to understand what was wrong and reality your advice worked
wtd




PostPosted: Sun Jun 01, 2008 4:01 pm   Post subject: RE:Nonstatic/static Referencing

Thanks. Glad you got something out of it.
Reality Check




PostPosted: Sun Jun 01, 2008 4:24 pm   Post subject: Re: Nonstatic/static Referencing

No problem man, that's why we're here Smile .
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: