illegal start of expression errors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • John Luttig

    illegal start of expression errors?

    Code:
     import java.awt.*;
    import java.awt.event.* ;
    import java.awt.image.*;
    import java.awt.geom.* ;
    import javax.swing.* ;
    import javax.swing.event.* ;
    
    public class NPCObject {
    	
    	public void NPCObject(int strength, int defense, boolean attackable, String imageFile, int x, int y) {
    
    int objStrength = strength;
    int objDefense = defense;
    boolean objAttackable = attackable;
    int xLoc = x;
    int yLoc = y;
    BufferedImage objIcon = loadImage(imageFile);
        
        public boolean canAttack() {
        	    
        	    return objAttackable;
        }
        
        int[] Location = new int[] {xLoc, yLoc};
        
        public int[] getLocation() {
        	    
        	    return Location;
        }
        
        public boolean getAttackable() {
        	    
        	    return objAttackable;
        }
        
        public int getStrength() {
        	    
        	    return objStrength;
        }
        
        public int getDefense() {
        	    
        	    return objDefense;
        }
        
        public BufferedImage getIcon() {
        	    
        	    return objIcon;
        }
    }
    public BufferedImage loadImage(String iconFile) {  
            BufferedImage icon = null;  
            try {  
      
                icon = ImageIO.read(new File(iconFile));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return icon;  
        }
    }
    and then I'm getting errors such as:


    NPCObject.java: 41: illegal start of expression
    public int getDefense() {
    ^
    NPCObject.java: 41: ';' expected
    public int getDefense() {
    ^
    NPCObject.java: 46: illegal start of expression
    public BufferedImage getIcon() {
    ^
    NPCObject.java: 46: ';' expected
    public BufferedImage getIcon() {

    I feel that this is probably a simple fix but I can't seem to understand what to do. Thanks for your help in advance.
  • JavierL
    New Member
    • Apr 2010
    • 17

    #2
    Declare the vars outside the scoop of the constructor.


    Something like this

    Code:
    public class NPCObject {
      int objStrength;     // Vars declared outside the scope of the constructor
      int objDefense;    // Otherwise they die after its execution
      boolean objAttackable;
      int xLoc;
      int yLoc;
      BufferedImage objIcon;
      public void NPCObject(int strength, int defense, boolean attackable, String imageFile, int x, int y) {
    
    	  objStrength = strength;  // Now vars defined inside the constructor
    	  objDefense = defense;
    	  objAttackable = attackable;
    	  xLoc = x;
    	  yLoc = y;
    	  objIcon = loadImage(imageFile);
      }
    
    .. more functions

    Comment

    Working...