473,416 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

illegal start of expression errors?

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*;
  2. import java.awt.event.* ;
  3. import java.awt.image.*;
  4. import java.awt.geom.* ;
  5. import javax.swing.* ;
  6. import javax.swing.event.* ;
  7.  
  8. public class NPCObject {
  9.  
  10.     public void NPCObject(int strength, int defense, boolean attackable, String imageFile, int x, int y) {
  11.  
  12. int objStrength = strength;
  13. int objDefense = defense;
  14. boolean objAttackable = attackable;
  15. int xLoc = x;
  16. int yLoc = y;
  17. BufferedImage objIcon = loadImage(imageFile);
  18.  
  19.     public boolean canAttack() {
  20.  
  21.             return objAttackable;
  22.     }
  23.  
  24.     int[] Location = new int[] {xLoc, yLoc};
  25.  
  26.     public int[] getLocation() {
  27.  
  28.             return Location;
  29.     }
  30.  
  31.     public boolean getAttackable() {
  32.  
  33.             return objAttackable;
  34.     }
  35.  
  36.     public int getStrength() {
  37.  
  38.             return objStrength;
  39.     }
  40.  
  41.     public int getDefense() {
  42.  
  43.             return objDefense;
  44.     }
  45.  
  46.     public BufferedImage getIcon() {
  47.  
  48.             return objIcon;
  49.     }
  50. }
  51. public BufferedImage loadImage(String iconFile) {  
  52.         BufferedImage icon = null;  
  53.         try {  
  54.  
  55.             icon = ImageIO.read(new File(iconFile));  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.         return icon;  
  60.     }
  61. }
  62.  
  63.  
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.
Oct 16 '10 #1
1 3091
JavierL
17
Declare the vars outside the scoop of the constructor.


Something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class NPCObject {
  3.   int objStrength;     // Vars declared outside the scope of the constructor
  4.   int objDefense;    // Otherwise they die after its execution
  5.   boolean objAttackable;
  6.   int xLoc;
  7.   int yLoc;
  8.   BufferedImage objIcon;
  9.   public void NPCObject(int strength, int defense, boolean attackable, String imageFile, int x, int y) {
  10.  
  11.       objStrength = strength;  // Now vars defined inside the constructor
  12.       objDefense = defense;
  13.       objAttackable = attackable;
  14.       xLoc = x;
  15.       yLoc = y;
  16.       objIcon = loadImage(imageFile);
  17.   }
  18.  
  19. .. more functions
  20.  
Oct 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: JSheble | last post by:
I've written a Windows Service using C#, but I cannot seem to figure out how to cancel or prevent the service from starting under certain conditions. For example, in the OnStart I read in some...
2
by: dobbouk | last post by:
I'm getting these 2 errors and haven't got a clue whats causing them please java 43: illegal start of type for (i=0;i< Number_of_squares; i++) java 215 identifier expected } ...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I have downloaded and installed the trial but the File New is greyed out so I can't get started! What have I not done that I need to do? --
0
by: afr0ninja | last post by:
I have set up a database on a shared network folder. I have a few users that log into it to post data. It also imports a few excel sheets that the database uses to crunch data with. I created...
6
by: aureao4 | last post by:
I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class...
1
by: biznitchil | last post by:
it is a bit jacked up, but copy and paste it into an IDE(i use jGRASP) plz help me...it worked just fine before I added the menu..you can e-mail me if you have any <Removed> import java.util.*; ...
4
by: rajesh619 | last post by:
I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors....
3
by: gokulagopal | last post by:
I am sending the code please help me in finding the error and correct it.The code is below: import java.awt.*; import java.awt.event.*; class CalcFrame extends Frame { ...
2
by: mole40 | last post by:
Hello! I'm very new to writing java. And I keep getting the "Illegal Start of Type" error. I've tried everything to fix it. can anyone help me? import java.util.*; class ClockTalk { public...
3
by: nickels | last post by:
Im not sure how to fix this compile error. Heres my code so far. import java.util.Scanner; public class test { public static int upper; public static int lower; public static boolean int...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.