Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: non-static variable

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default non-static variable

    heres my error:
    .\palidino76\rs2\io\packets\ObjectOption1.java:59: non-static variable Portal ca
    nnot be referenced from a static context
                            CastleWars.Portal = 1;
                                      ^
    .\palidino76\rs2\io\packets\ObjectOption1.java:60: non-static method Enter() can
    not be referenced from a static context
                            CastleWars.Enter();
                                      ^
    .\palidino76\rs2\content\minigames\CastleWars.java:122: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addSara(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:124: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addZamy(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:126: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addGuthix(p);
                                      ^
    .\palidino76\rs2\content\minigames\CastleWars.java:141: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                    checkPlayer(p);
                                ^
    6 errors
    Press any key to continue . . .

    ObjectOption1 is not a static class nor does it have any static methods.. heres the class
    package palidino76.rs2.io.packets;
     
    import palidino76.rs2.players.Player;
    import palidino76.rs2.util.Misc;
    import palidino76.rs2.content.minigames.*;
     
    public class ObjectOption1 implements Packet {
        /*
         * make sure to document EVERY coordinate to go with each object unless an un-important object(wilderness ditch lol).
         * This will prevent people from spawning an object client side and actually using it.
         * So make sure to include with the id, objectX == # && objectY == #
        */
     
        /**
         * Handles the first option on objects.
         * @param p The Player which the frame should be handled for.
         * @param packetId The packet id this belongs to.
         * @param packetSize The amount of bytes being recieved for this packet.
         */
        public void handlePacket(Player p, int packetId, int packetSize) {
            if (p == null || p.stream == null) {
                return;
            }
            if (!p.objectOption1) {
                p.clickX = p.stream.readUnsignedWordBigEndian();
                p.clickId = p.stream.readUnsignedWord();
                p.clickY = p.stream.readUnsignedWordBigEndianA();
                if (Misc.getDistance(p.absX, p.absY, p.clickX, p.clickY) > 30) {
                    return;
                }
                p.objectOption1 = true;
            }
            int distance = Misc.getDistance(p.clickX, p.clickY, p.absX, p.absY);
            if (p.walkDir != -1 || p.runDir != -1 || distance > objectSize(p.clickId)) {
                return;
            }
            p.objectOption1 = false;
            switch (p.clickId) {
            default:
                Misc.println("[" + p.username + "] Unhandled object 1: " + p.clickId);
                break;
            }
        }
     
        private int objectSize(int id) {
            switch (id) {
     
    		case 4387:
    			CastleWars.Portal = 1;
    			CastleWars.Enter();
    		break;
     
            default:
                return 1;
            }
        }
    }

    heres my castlewars class:
    package palidino76.rs2.content.minigames;
     
    import palidino76.rs2.players.Player;
    import palidino76.rs2.Engine;
    import palidino76.rs2.io.*;
     
    public class CastleWars {
     
    public int 
    Portal = 0,
    GameTimer = 600,
    SaraTeam = 0,
    ZamyTeam = 0,
    SaraScore = 0,
    ZamyScore = 0;
     
    public boolean 
    IsSara = false,
    IsZamy = false,
    IsClean = false,
    BlueSafe = false,
    RedSafe = false;
     
    public boolean hasBadItem(Player p) {
     
    	if (Engine.playerItems.HasItemAmount(p, 1052, 1) //capes
    		|| Engine.playerItems.HasItemAmount(p, 6570, 1) || Engine.playerItems.HasItemAmount(p, 6568, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9747, 1) || Engine.playerItems.HasItemAmount(p, 9748, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9750, 1) || Engine.playerItems.HasItemAmount(p, 9751, 1) 
    		|| Engine.playerItems.HasItemAmount(p, 9753, 1) || Engine.playerItems.HasItemAmount(p, 9754, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9756, 1) || Engine.playerItems.HasItemAmount(p, 9757, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9759, 1) || Engine.playerItems.HasItemAmount(p, 9760, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9762, 1) || Engine.playerItems.HasItemAmount(p, 9763, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9765, 1) || Engine.playerItems.HasItemAmount(p, 9766, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9767, 1) || Engine.playerItems.HasItemAmount(p, 9769, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9771, 1) || Engine.playerItems.HasItemAmount(p, 9772, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9774, 1) || Engine.playerItems.HasItemAmount(p, 9775, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9777, 1) || Engine.playerItems.HasItemAmount(p, 9778, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9780, 1) || Engine.playerItems.HasItemAmount(p, 9781, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9783, 1) || Engine.playerItems.HasItemAmount(p, 9784, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9786, 1) || Engine.playerItems.HasItemAmount(p, 9787, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9789, 1) || Engine.playerItems.HasItemAmount(p, 9790, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9792, 1) || Engine.playerItems.HasItemAmount(p, 9793, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9795, 1) || Engine.playerItems.HasItemAmount(p, 9796, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9798, 1) || Engine.playerItems.HasItemAmount(p, 9799, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9801, 1) || Engine.playerItems.HasItemAmount(p, 9802, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9804, 1) || Engine.playerItems.HasItemAmount(p, 9805, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9807, 1) || Engine.playerItems.HasItemAmount(p, 9808, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9810, 1) || Engine.playerItems.HasItemAmount(p, 9811, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9813, 1) //end capes
     
    		//hats and helms
    		|| Engine.playerItems.HasItemAmount(p, 1038, 1) || Engine.playerItems.HasItemAmount(p, 1040, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1042, 1) || Engine.playerItems.HasItemAmount(p, 1044, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1046, 1) || Engine.playerItems.HasItemAmount(p, 1048, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1050, 1) || Engine.playerItems.HasItemAmount(p, 1053, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1055, 1) || Engine.playerItems.HasItemAmount(p, 1057, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1149, 1) || Engine.playerItems.HasItemAmount(p, 1155, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1157, 1) || Engine.playerItems.HasItemAmount(p, 1159, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1161, 1) || Engine.playerItems.HasItemAmount(p, 1163, 1)
    		|| Engine.playerItems.HasItemAmount(p, 1165, 1) || Engine.playerItems.HasItemAmount(p, 4716, 1)
    		|| Engine.playerItems.HasItemAmount(p, 4708, 1) || Engine.playerItems.HasItemAmount(p, 4724, 1)
    		|| Engine.playerItems.HasItemAmount(p, 4732, 1) || Engine.playerItems.HasItemAmount(p, 4745, 1)
    		|| Engine.playerItems.HasItemAmount(p, 4753, 1) || Engine.playerItems.HasItemAmount(p, 11335, 1)
    		|| Engine.playerItems.HasItemAmount(p, 10828, 1) || Engine.playerItems.HasItemAmount(p, 10334, 1)
    		|| Engine.playerItems.HasItemAmount(p, 10342, 1) || Engine.playerItems.HasItemAmount(p, 10350, 1)
    		|| Engine.playerItems.HasItemAmount(p, 2639, 1) || Engine.playerItems.HasItemAmount(p, 2641, 1)
    		|| Engine.playerItems.HasItemAmount(p, 2743, 1) || Engine.playerItems.HasItemAmount(p, 2581, 1)
    		|| Engine.playerItems.HasItemAmount(p, 12210, 1) || Engine.playerItems.HasItemAmount(p, 12213, 1)
    		|| Engine.playerItems.HasItemAmount(p, 12216, 1) || Engine.playerItems.HasItemAmount(p, 12219, 1)
    		|| Engine.playerItems.HasItemAmount(p, 12222, 1) || Engine.playerItems.HasItemAmount(p, 9749, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9752, 1) || Engine.playerItems.HasItemAmount(p, 9755, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9758, 1) || Engine.playerItems.HasItemAmount(p, 9761, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9764, 1) || Engine.playerItems.HasItemAmount(p, 9767, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9770, 1) || Engine.playerItems.HasItemAmount(p, 9773, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9776, 1) || Engine.playerItems.HasItemAmount(p, 9779, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9782, 1) || Engine.playerItems.HasItemAmount(p, 9785, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9788, 1) || Engine.playerItems.HasItemAmount(p, 9791, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9794, 1) || Engine.playerItems.HasItemAmount(p, 9797, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9800, 1) || Engine.playerItems.HasItemAmount(p, 9803, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9806, 1) || Engine.playerItems.HasItemAmount(p, 9809, 1)
    		|| Engine.playerItems.HasItemAmount(p, 9812, 1) || Engine.playerItems.HasItemAmount(p, 9814, 1)) {
    			return true;
    		} else {
    			return false;
    		}
    	}
     
    	public void addGuthix(Player p) {
    		if (ZamyTeam > SaraTeam) {
    			addSara(p);
    		}
    		if (SaraTeam > ZamyTeam) {
    			addZamy(p);
    		}
    	}
     
    	public void addZamy(Player p) {
    		ZamyTeam += 1;
    		IsZamy = true;
    		p.frames.sendMessage(p, "You will fight for Zamorak!");
    	}
     
    	public void addSara(Player p) {
    		SaraTeam += 1;
    		IsSara = true;
    		p.frames.sendMessage(p, "You will fight for Saradomin!");
    	}
     
    	public void handlePortal() {
    		if (Portal == 1) {
    			addSara(p);
    		} else if (Portal == 2) {
    			addZamy(p);
    		} else if  (Portal == 3) {
    			addGuthix(p);
    		}
    	}
     
    	public void checkPlayer(Player p) {
    		if (p.equipment[1] > 0 && p.equipmentN[1] > 0 || p.equipment[0] > 0 && p.equipmentN[0] > 0 || hasBadItem(p) == true) {
    			p.frames.sendMessage(p, "You cannot bring capes, hats, or helmets into the game!");
    			IsClean = false;
    		} else {
    			IsClean = true;
    			handlePortal();	
    		}
    	}
     
    	public void Enter() {
    		checkPlayer(p);
    	}
     
    	public void inGame(Player p) {
    			p.frames.setOverlay(p, 58);
    		if (BlueSafe == false) {
    			p.frames.setString(p, "Safe", 58, 4);
    		} else {
    			p.frames.setString(p, "<col=ff0000>Taken", 58, 4);
    		}
    		if (RedSafe == false) {
    			p.frames.setString(p, "Safe", 58, 5);
    		} else {
    			p.frames.setString(p, "<col=ff0000>Taken", 58, 5);
    		}
    			p.frames.setString(p, "Zamorak: "+ZamyScore, 58, 0);
    			p.frames.setString(p, "Saradomin: "+SaraScore, 58, 1);
    			p.frames.setString(p, ""+GameTimer, 58, 8);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non-static variable

    CastleWars.Portal
    This is the syntax for referring to a static variable in the CastleWars class. To refer to an instance variable, create an instance of the class and use that.
    CastleWars anInstanceOfCastleWars = new CastleWars(); // create an instance
    anInstanceOfCastleWars.Portal // refer to the Portal variable in that instance

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    ok i did this:
    public CastleWars castleWars = new CastleWars();

    now im getting this error:
    .\palidino76\rs2\content\minigames\CastleWars.java:122: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addSara(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:124: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addZamy(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:126: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addGuthix(p);
                                      ^
    .\palidino76\rs2\content\minigames\CastleWars.java:141: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                    checkPlayer(p);
                                ^
    .\palidino76\rs2\io\packets\ObjectOption1.java:59: package Engine does not exist
     
                            Engine.castleWars.Portal = 1;
                                  ^
    .\palidino76\rs2\io\packets\ObjectOption1.java:60: package Engine does not exist
     
                            Engine.castleWars.Enter();
                                  ^
    6 errors
    Press any key to continue . . .

  4. #4
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    ok i fixed that now im down to 4 errors

    .\palidino76\rs2\content\minigames\CastleWars.java:122: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addSara(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:124: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addZamy(p);
                                    ^
    .\palidino76\rs2\content\minigames\CastleWars.java:126: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                            addGuthix(p);
                                      ^
    .\palidino76\rs2\content\minigames\CastleWars.java:141: cannot find symbol
    symbol  : variable p
    location: class palidino76.rs2.content.minigames.CastleWars
                    checkPlayer(p);
                                ^
    4 errors
    Press any key to continue . . .

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non-static variable

    Where is the variable p defined? Is it in scope on the lines where you are trying to use it? 122, 124, 126, 141

  6. #6
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    no its defined here

    public void addGuthix(Player p) {
    public void addZamy(Player p) {
    public void addSara(Player p) {
    public void checkPlayer(Player p) {
    public void inGame(Player p) {

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non-static variable

    Is it defined where you are trying to use it?

  8. #8
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    yes. it was working before and i haven't changed anything in that class

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non-static variable

    i haven't changed anything in that class
    I wish I had a dollar for every time I've heard that one.

    The compiler doesn't think so. It can't find the definition for p in the four places shown in the error message.

  10. #10
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    i never changed it i just never finished it 0.o lol i got it fixed now thank you

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: non-static variable

    i just never finished it
    Then you were getting the error messages before.

  12. #12
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: non-static variable

    no i added on to it at the same time i did the other stuff and the errors were together so yea..

Similar Threads

  1. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM
  2. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  3. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  4. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  5. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM

Tags for this Thread